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

JAVA发送HttpClient请求及接收请求结果过程

HttpClient使用详解

代码如下:

    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参考如下

httpClient 超时时间设置(转)

java使用HttpClient的更多相关文章

  1. Java通过httpclient获取cookie模拟登录

    package Step1; import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.Htt ...

  2. Java中httpClient中三种超时设置

    本文章给大家介绍一下关于Java中httpClient中的三种超时设置小结 在Apache的HttpClient包中,有三个设置超时的地方: /* 从连接池中取连接的超时时间*/ ConnManage ...

  3. Java使用HttpClient上传文件

    Java可以使用HttpClient发送Http请求.上传文件等,非常的方便 Maven <dependency> <groupId>org.apache.httpcompon ...

  4. Java的HttpClient的实现

    HttpClient的概念就是模仿浏览器请求服务端内容,也可以做App和Server之间的链接. 这个是关于Java的HttpClient的简单实例,其实java本身也可以通过自己的net包去做,但是 ...

  5. java 通过httpclient调用https 的webapi

    java如何通过httpclient 调用采用https方式的webapi?如何验证证书.示例:https://devdata.osisoft.com/p...需要通过httpclient调用该接口, ...

  6. 基于maven+java+TestNG+httpclient+poi+jsonpath+ExtentReport的接口自动化测试框架

    接口自动化框架 项目说明 本框架是一套基于maven+java+TestNG+httpclient+poi+jsonpath+ExtentReport而设计的数据驱动接口自动化测试框架,TestNG ...

  7. Java之HttpClient调用WebService接口发送短信源码实战

    摘要 Java之HttpClient调用WebService接口发送短信源码实战 一:接口文档 二:WSDL 三:HttpClient方法 HttpClient方法一 HttpClient方法二 Ht ...

  8. java实现httpclient 访问

    package com.test; import java.io.File; import java.io.FileInputStream; import java.io.IOException; i ...

  9. java 封装httpclient 的get 和post 请求

    import java.io.ByteArrayOutputStream; import java.io.UnsupportedEncodingException; import java.util. ...

随机推荐

  1. 短网址(short URL)系统的原理及其实现

    短网址(short URL)系统的原理及其实现 https://hufangyun.com/2017/short-url/?hmsr=toutiao.io&utm_medium=toutiao ...

  2. MQ内存消耗与积压分析

    [root@iZ23nn1p4mjZ logs]# rabbitmqctl status Status of node rabbit@iZ23nn1p4mjZ ... [{pid,15425}, {r ...

  3. JavaScript 实现省市二级联动

    JavaScript 实现省市二级联动 版权声明:未经授权,严禁转载! 案例代码 <style> .hide { display: none; } </style> <s ...

  4. 20145216史婧瑶《网络对抗》逆向及Bof进阶实践

    20145216史婧瑶<网络对抗>逆向及Bof进阶实践 基础知识 Shellcode实际是一段代码,但却作为数据发送给受攻击服务器,将代码存储到对方的堆栈中,并将堆栈的返回地址利用缓冲区溢 ...

  5. 20145330 《网络攻防》 MSF基础应用

    20145330 <网络攻防> MSF基础应用 1.实验后回答问题 (1)用自己的话解释什么是exploit,payload,encode. exploit:进行渗透攻击的模块合集 pay ...

  6. hash-补做

    题目 利用除留余数法为下列关键字集合的存储设计hash函数,并画出分别用开放寻址法和拉链法解决冲突得到的空间存储状态(散列因子取0.75) 关键字集合:85,75,57,60,65,(你的8位学号相加 ...

  7. Python3基础 set 删除list中的重复项

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  8. Python3基础 while 循环示例

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  9. 动态规划模板1|LIS最长上升子序列

    LIS最长上升子序列 dp[i]保存的是当前到下标为止的最长上升子序列的长度. 模板代码: int dp[MAX_N], a[MAX_N], n; int ans = 0; // 保存最大值 for ...

  10. The equation (扩展欧几里得)题解

    There is an equation ax + by + c = 0. Given a,b,c,x1,x2,y1,y2 you must determine, how many integer r ...