HttpClient发送后台请求
2015-10-28 10:09
381 查看
由于API的不断更新,所以创建HttpClient对象和设置超时代理方式也会有细微区别
详细变更请参考API文档,这里直接将实例
// 3.X版本 HttpClient httpClient=new DefaultHttpClient(); httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,2000);//连接超时时间,毫秒 //4.3版本 CloseableHttpClient httpClient = HttpClients.createDefault(); //或者使用HttpClientBuilder对象 // 创建HttpClientBuilder HttpClientBuilder httpClientBuilder=HttpClientBuilder.create(); // HttpClient CloseableHttpClient closeableHttpClient = httpClientBuilder.build(); //请求地址 HttpPost post = new HttpPost(url); //设置代理和超时 RequestConfig config=RequestConfig.custom().setConnectionRequestTimeout(30000).setProxy(httpProxyHost).build(); post.setConfig(config);
详细变更请参考API文档,这里直接将实例
/** * web 请求/响应 API类 */ public class WebUtil { /** * post 请求 * * @param url * 请求路径 * @param params * 请求参数 * @return 请求返回 */ public String post(String url, List<BasicNameValuePair> params) { PayLog.addLog(ELogAction.Post, EPayLogType.PostRequest, new Object[] { url, params }); if (CheckValue.valideteNullOrEmpty(url) || params == null || params.isEmpty()) { return ""; } String result = ""; ProxyUtil proxyUtil=HttpRequestHelper.getProxyUtil(); // 创建HttpClientBuilder HttpClientBuilder httpClientBuilder=HttpClientBuilder.create(); // HttpClient CloseableHttpClient closeableHttpClient = httpClientBuilder.build(); //请求地址 HttpPost post = new HttpPost(url); RequestConfig config=null; //如果需要设置代理 if (proxyUtil.getIsEnable()) { HttpHost httpProxyHost=new HttpHost(proxyUtil.getHost(),proxyUtil.getPort(),"http"); config=RequestConfig.custom().setConnectionRequestTimeout(30000) .setProxy(httpProxyHost).build(); } else { //超时时间设置为30s config=RequestConfig.custom().setConnectionRequestTimeout(30000).build(); } //设置请求超时时间和有可能设置代理 post.setConfig(config); try { post.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8)); CloseableHttpResponse response = closeableHttpClient.execute(post); result = parseResponse(response.getEntity().getContent()); PayLog.addLog(ELogAction.Post, EPayLogType.PostResponse, result); // 释放资源 closeableHttpClient.close(); } catch (Exception ex) { PayLog.addErrorLog(ELogAction.Post, EPayLogType.PostResponse, ex.getMessage()); result = "未知错误,请联系客服!"; } return result; } /** * 解析Response信息 * * @param respStream * response stream * @return */ protected String parseResponse(InputStream respStream) { BufferedReader reader = new BufferedReader(new InputStreamReader(respStream)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line); } } catch (IOException e) { e.printStackTrace(); } finally { try { respStream.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } /** * 将Response信息转为Json格式 * * @param response * eg:param1=v1¶m2=v2¶m3=v3 ... * @return */ public String responseToJson(String response) { if (CheckValue.valideteNullOrEmpty(response)) { return ""; } Map<String, String> map = new HashMap<String, String>(); String[] responseParams = response.split("&"); String key = ""; String value = ""; for (String string : responseParams) { String[] keyValues = string.split("="); if (keyValues != null && keyValues.length > 0) { key = keyValues[0]; } if (keyValues != null && keyValues.length > 1) { value = keyValues[1]; } else { value = ""; } map.put(key, value); } Gson gson = new Gson(); return gson.toJson(map); } }
相关文章推荐
- Spring3.2异步处理http请求
- 配置Tomcat使用https协议(配置SSL协议)
- 【Java TCP/IP Socket】构建和解析自定义协议消息(含代码)
- MTK平台下用Socket如何实现HTTP请求
- 计算机网络-http简介
- IDEA maven web 问题 -- maven包由于网络问题不能自动下载--本地包无法识别-解决
- https原理及tomcat配置https方法
- HTTP协议中的8种方法:Options,Head,Get,Post,put,Delete,Trace,Connect
- http的post传送文件原理
- ashx一般处理程序和HttpHandler
- 【Android测试技巧】02. 测试手机客户端HTTPS 你应该知道的事
- 机器学习、统计分析、数据挖掘、神经网络、人工智能、模式识别之间的关系是什么?
- TCP/IP,http,socket,长连接,短连接
- 四种常见的 POST 提交数据方式
- 修复Windows XP/2003 系统TCP/IP网络协议
- Tomcat处理HTTP请求源码分析--包含jsp内置对象的实例过程
- tmcat和appach http 服务器分别是用什么语言编写的
- tomcat之jsp/servlet容器与tomcat与专业的http服务器的区别
- python实现socket网络通信
- 承接游戏UI美术外包【厦门巨游网络科技有限公司】