HttpClient常用的包有两个
org.apache.http.client以及org.apache.commons.httpclient
我常用的是org.apache.http.client。
HttpClient在4.3版本以后声明HttpClient的方法和以前略有区别,不再是直接声明new DefaultHttpClient() .
参考下文:
new DefaultHttpClient过时处理建议和HTTP调用后关闭流处理
HttpClient 4.5.2版本设置连接超时时间-CloseableHttpClient设置Timeout
代码如下:
public static void sendHttpAudit(OAApplicationModule am, String type, String entityMappingId, String param) { System.out.println(System.currentTimeMillis()); String oaRestfulServiceUrl = null; // am.getOADBTransaction().getProfile(""); oaRestfulServiceUrl = "http://xxx.xxx.xxx.xxx:8090/api/Service/submitXXX"; // RequestConfig requestConfig = // RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(1000).setSocketTimeout(5000).build(); RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(1000).build(); CloseableHttpClient httpclient = null; CloseableHttpResponse h_response = null; try { httpclient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build(); // httpclient = HttpClientBuilder.create().build(); HttpPost postMethod = new HttpPost(oaRestfulServiceUrl); // postMethod.setConfig(requestConfig); postMethod.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36"); postMethod.setHeader("Referer", oaRestfulServiceUrl); List<NameValuePair> params = new ArrayList<NameValuePair>(); //200_ifaceCode 200表示为EBS系统调用RESTFUL服务。 params.add(new BasicNameValuePair("type", type)); params.add(new BasicNameValuePair("param", param)); //添加参数 postMethod.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); h_response = httpclient.execute(postMethod); HttpEntity repEntity = h_response.getEntity(); int statusCode = h_response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { postMethod.abort(); procProcessError(am, entityMappingId, String.valueOf(statusCode), EntityUtils.toString(repEntity, "UTF-8") ); throw new OAException("HttpClient,error status code :" + statusCode); } String content = EntityUtils.toString(repEntity, "UTF-8"); System.out.println(content); JSONObject json = JSON.parseObject(content); String retCode = json.getString("returnCode"); String retMsg = json.getString("returnMsg"); procProcessError(am, entityMappingId, retCode, retMsg ); if (!"000000".equals(retCode)) { System.out.println(" error " +retMsg ); throw new OAException("接口服务处理异常" + json.getString("returnMsg")); } else { changeStatusInprocess(am, entityMappingId); } } catch (ConnectTimeoutException e) { procProcessError(am, entityMappingId, "ConnectTimeoutException", e.getMessage() ); System.out.println(" ConnectTimeoutException " + System.currentTimeMillis()); e.printStackTrace(); throw new OAException("接口服务处理异常 ConnectTimeoutException " + e.getMessage()); } catch (SocketTimeoutException e) { procProcessError(am, entityMappingId, "SocketTimeoutException", e.getMessage() ); System.out.println(" SocketTimeoutException " + System.currentTimeMillis()); e.printStackTrace(); throw new OAException("接口服务处理异常 SocketTimeoutException " + e.getMessage()); } catch (Exception e) { procProcessError(am, entityMappingId, "Exception", e.getMessage() ); System.out.println("Exception " + System.currentTimeMillis()); e.printStackTrace(); throw new OAException("接口服务处理异常 Exception " + e.getMessage()); } finally { if (h_response != null) { try { h_response.close(); } catch (IOException e) { e.printStackTrace(); } } if (httpclient != null) { try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } } }
org.apache.commons.httpclient参考如下
分类:
Java
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· Apache Tomcat RCE漏洞复现(CVE-2025-24813)
2016-11-16 对EBS中配置文件的初步认识