HttpClient request payload post请求(传输的数据为json)
来源:互联网 发布:登陆淘宝网 编辑:程序博客网 时间:2024/06/13 21:14
private static String postJsonFile1(String url, Map params, Map<String, String> headers, int connectTimeout, int readTimeout, String encoding, HttpUtil.HttpMethod method) { JSONObject jsonObject = JSONObject.fromObject(params); URL uUrl = null; HttpURLConnection conn = null; BufferedWriter out = null; BufferedReader in = null; try { //创建和初始化连接 uUrl = new URL(url); conn = (HttpURLConnection) uUrl.openConnection(); conn.setRequestProperty("content-type", "application/json"); conn.setRequestMethod(method.toString()); conn.setDoOutput(true); conn.setDoInput(true); //设置连接超时时间 conn.setConnectTimeout(connectTimeout); //设置读取超时时间 conn.setReadTimeout(readTimeout); //指定请求header参数 if (headers != null && headers.size() > 0) { Set<String> headerSet = headers.keySet(); for (String key : headerSet) { conn.setRequestProperty(key, headers.get(key)); } } out = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), encoding)); out.write(jsonObject.toString()); out.flush(); //接收返回结果 StringBuilder result = new StringBuilder(); in = new BufferedReader(new InputStreamReader(conn.getInputStream(), encoding)); if (in != null) { String line = ""; while ((line = in.readLine()) != null) { result.append(line); } } return result.toString(); } catch (Exception e) { logger.error("调用接口[" + url + "]失败!请求URL:" + url + ",参数:" + jsonObject, e); System.out.println("发送POST请求出现异常!" + e); //处理错误流,提高http连接被重用的几率 try { byte[] buf = new byte[100]; InputStream es = conn.getErrorStream(); if (es != null) { while (es.read(buf) > 0) { ; } es.close(); } } catch (Exception e1) { e1.printStackTrace(); } } finally { try { if (out != null) { out.close(); } } catch (Exception e) { e.printStackTrace(); } try { if (in != null) { in.close(); } } catch (Exception e) { e.printStackTrace(); } //关闭连接 if (conn != null) { conn.disconnect(); } } return null; }
阅读全文
0 0
- HttpClient request payload post请求(传输的数据为json)
- HttpClient request payload post请求
- HttpClient request payload post请求
- Vue-resource中post请求将data数据以request payload转换为form data的形式
- 关于HttpClient post请求 传输json格式的字节流
- httpclient的post请求并处理json格式响应数据
- httpClient 发送 post请求 json数据
- HttpClient post请求 发送Json数据
- http post payload request类型请求
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- HttpClient --post请求数据
- http请求远程接口获得Json数据(2) --- 利用HttpClient发送post/get请求
- HttpClient的get和post请求数据
- httpclient模拟post请求json封装表单数据
- vue axios POST请求中参数以form data和request payload形式的原因
- HttpClient请求json数据
- 58. Length of Last Word
- 【BZOJ】3668 [Noi2014]起床困难综合症 贪心
- ls 命令的实用范例
- 浅谈 Linux 内核开发之网络设备驱动
- vb.net 教程 8-3 数据库操作6
- HttpClient request payload post请求(传输的数据为json)
- Capstone训练营第六天
- apache 优化配置 prefork模式
- 使用spring实现读写分离(mysql主从复制)二-配置主从复制
- Oracle中的job定时任务
- http请求上传文件
- 兄弟连学python》》》什么是变量
- win7安装pip、matplotlib模块
- 使用UltraEdit进行SSH远程连接