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

推荐订阅源

IT之家
IT之家
J
Java Code Geeks
小众软件
小众软件
Jina AI
Jina AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Hugging Face - Blog
Hugging Face - Blog
Stack Overflow Blog
Stack Overflow Blog
Blog — PlanetScale
Blog — PlanetScale
C
Check Point Blog
人人都是产品经理
人人都是产品经理
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - Franky
Apple Machine Learning Research
Apple Machine Learning Research
G
Google Developers Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The GitHub Blog
The GitHub Blog
腾讯CDC
T
The Blog of Author Tim Ferriss
大猫的无限游戏
大猫的无限游戏
量子位
M
MIT News - Artificial intelligence
Last Week in AI
Last Week in AI
L
LangChain Blog

博客园 - 我的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;
	}