惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
Security Latest
Security Latest
S
Secure Thoughts
H
Heimdal Security Blog
The Last Watchdog
The Last Watchdog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security Affairs
PCI Perspectives
PCI Perspectives
C
CERT Recently Published Vulnerability Notes
C
CXSECURITY Database RSS Feed - CXSecurity.com
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
爱范儿
爱范儿
腾讯CDC
P
Privacy & Cybersecurity Law Blog
量子位
T
Threat Research - Cisco Blogs
V
V2EX
S
Schneier on Security
P
Proofpoint News Feed
美团技术团队
Apple Machine Learning Research
Apple Machine Learning Research
Forbes - Security
Forbes - Security
The Hacker News
The Hacker News
月光博客
月光博客
C
Cybersecurity and Infrastructure Security Agency CISA
Schneier on Security
Schneier on Security
L
LINUX DO - 最新话题
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
Visual Studio Blog
S
SegmentFault 最新的问题
G
GRAHAM CLULEY
人人都是产品经理
人人都是产品经理
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园 - 叶小钗
罗磊的独立博客
WordPress大学
WordPress大学
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cisco Blogs
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tenable Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
W
WeLiveSecurity
V
Vulnerabilities – Threatpost
Last Week in AI
Last Week in AI
Google DeepMind News
Google DeepMind News
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

博客园 - 我的bug

项目引入同一jar包不同版本处理 【笔记】redis实现类 【笔记】websockt一对一聊天java部分 【笔记】vue中websocket心跳机制 【笔记】MySQL删除重复记录保留一条 oss上传实例 jquery实现图片点击旋转 IDEA卡顿解决方法 斐波那契数列 【笔记】获取新浪财经最新的USDT-CNY的汇率 【笔记】Java 信任所有SSL证书(解决PKIX path building failed问题) 【笔记】jquery判断两个日期之间相差多少天 【笔记】spring定时器时间配置实例 【笔记】jquery加减乘除及科学计算法处理 string 日期相加和相减 sql查询慢原因及优化 java小算法复习 找出一组数字中出现最多的字符 【转载】js清空cookie
【笔记】接口发送数据及接收
我的bug · 2018-07-25 · via 博客园 - 我的bug

发送数据

 /**
     * 
     * @param httpUrl
     * @param param
     */
    public static void postSend(String httpUrl,String param){
    	try{
			URL url = new URL(httpUrl);
			SslUtils.ignoreSsl();
	       	HttpURLConnection connection = (HttpURLConnection) url.openConnection();
	       	connection.setDoInput(true);
	       	connection.setDoOutput(true);
	       	connection.setRequestMethod("POST");
	       	connection.setRequestProperty("connection", "keep-alive");
	       	connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
	       	connection.setConnectTimeout(3000);
	       	connection.setReadTimeout(3000);
	       	connection.setUseCaches(false);
	       	//post请求
	       	OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "utf-8");
	       	String json = param.toString();
	       	out.write(param.toCharArray(),0,param.length());
	       	System.out.println(json);
            out.flush();
            out.close();
            // 读取响应 
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String lines;
            StringBuffer sb = new StringBuffer("");
			while ((lines = reader.readLine()) != null) {
				lines = new String(lines.getBytes(), "utf-8");
				sb.append(lines);
			}
			JSONObject jsStr =JSONObject.parseObject(sb.toString());
			//获取响应值,判断是否验证通过
			String code = (String) jsStr.get("code");
			String msg=(String) jsStr.get("msg");
			System.out.println("code:"+code+",msg:"+msg);
			String result = null;
			//接口返回验证数据是否通过
			if("0".equals(code)){
				result = "success";
			} else{
				result = "fail";
				System.out.println("下发出错:错误原因为" + msg + "下发内容为:" + json);
			}        
			reader.close();
			// 断开连接 
			connection.disconnect();
    	}catch(Exception e){
    		e.printStackTrace();
    	}
	}

  接收

String param = accept(request);

public static String accept(HttpServletRequest request){
		// 接收传过来的参数
		BufferedInputStream bufferedInputStream = null;
		// 此类实现了一个输出流,其中的数据被写入一个字节数组
		ByteArrayOutputStream bytesOutputStream = null;
		String result = null;
		try
		{
			// BufferedInputStream 输入流
			bufferedInputStream = new BufferedInputStream (request.getInputStream ());
			bytesOutputStream = new ByteArrayOutputStream();
			// 写入数据
			int ch;
			while ((ch = bufferedInputStream.read ()) != -1)
			{
				bytesOutputStream.write (ch);
			}
			// 转换为String类型
			result = new String (bytesOutputStream.toByteArray (),"UTF-8");
		}catch (Exception ex){
			ex.printStackTrace ();
		}
		return result;
	}