Java HttpClient4.2.x版本get、post请求工具类

2018-11-22 08:42:13来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

公司业务需要,跟很多公司合作,经常需要请求外部http接口,而项目架构是一个比较老的框架整合,仅http请求的工具类就很多个,显得杂乱无章;

有些接口测试时,对方做了IP白名单限制的,ZIP压缩等要求,现有的http工具类无法满足要求,又不能去修改,因为很多地方在用;想引入最新HttpClient版本的依赖,确发现与现有的jar包冲突;

无耐只能使用现有的jar重新封装,具体代码演示如下:

post请求方式1

/**
* POST请求,超时时间必须设置
* @param url
* @param json JSON数据格式传参
* @return
*/
public static String post(String url, String json) {
String result = null;
HttpClient httpClient = null;
HttpPost httpPost = null;

try {
httpClient = new DefaultHttpClient();
httpPost = new HttpPost(url);
// 连接超时设置
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 15000);
// 读取超时设置
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);

StringEntity stringEntity = new StringEntity(json, Consts.UTF_8);
stringEntity.setContentEncoding(Consts.UTF_8.toString());
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);

HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
if (null != entity) {
result = EntityUtils.toString(entity, Consts.UTF_8);
}
} catch (Exception e) {
e.printStackTrace();
result = JSON.toJSONString(Result.error(e.getMessage()));
} finally {
// 终止本次请求
httpPost.abort();
// 释放连接
httpPost.releaseConnection();
httpClient.getConnectionManager().shutdown();
}

return result;
}

 

post请求方式2

/**
* POST请求,超时时间必须设置
* @param url
* @param params
* @return
*/
public static String post(String url, Map<String, String> params) {
String result = null;
HttpClient httpClient = null;
HttpPost httpPost = null;

try {
List<NameValuePair> pairs = new ArrayList<>();
for (Entry<String, String> entry : params.entrySet()) {
pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
httpClient = new DefaultHttpClient();
httpPost = new HttpPost(url);
// 连接超时设置
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 15000);
// 读取超时设置
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);

StringEntity stringEntity = new UrlEncodedFormEntity(pairs, Consts.UTF_8);
httpPost.setEntity(stringEntity);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
httpPost.setHeader("Accept", "text/plain;charset=utf-8");
httpPost.setHeader("Accept-Encoding", "gzip, deflate");
httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36");
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
if (null != entity) {
result = EntityUtils.toString(entity, Consts.UTF_8);
}
} catch (Exception e) {
e.printStackTrace();
result = JSON.toJSONString(Result.error(e.getMessage()));
} finally {
// 终止本次请求
httpPost.abort();
// 释放连接
httpPost.releaseConnection();
httpClient.getConnectionManager().shutdown();
}

return result;
}

 

 

get请求

/**
* GET请求,超时时间必须设置
* @param url
* @param params Map<String, String>数据格式传参
* @return
*/
public static String get(String url, Map<String, String> params) {
String result = null;
HttpClient httpClient = null;
HttpGet httpGet = null;

try {
List<NameValuePair> pairs = new ArrayList<>();
for (Entry<String, String> entry : params.entrySet()) {
pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
URI uri = new URI(url + "?" + URLEncodedUtils.format(pairs, Consts.UTF_8));

httpClient = new DefaultHttpClient();
httpGet = new HttpGet(uri);

// 连接超时设置
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 15000);
// 读取超时设置
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);

HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (null != entity) {
result = EntityUtils.toString(entity, Consts.UTF_8);
}
} catch (Exception e) {
e.printStackTrace();
result = JSON.toJSONString(Result.error(e.getMessage()));
} finally {
httpGet.abort();
httpGet.releaseConnection();
httpClient.getConnectionManager().shutdown();
}

return result;
}

 

post请求代理方式

/**
* GET请求,超时时间必须设置
* @param url
* @param params Map<String, String>数据格式传参
* @return
*/
public static String get(String url, Map<String, String> params) {
String result = null;
HttpClient httpClient = null;
HttpGet httpGet = null;

try {
List<NameValuePair> pairs = new ArrayList<>();
for (Entry<String, String> entry : params.entrySet()) {
pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
URI uri = new URI(url + "?" + URLEncodedUtils.format(pairs, Consts.UTF_8));

httpClient = new DefaultHttpClient();
httpGet = new HttpGet(uri);

// 连接超时设置
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 15000);
// 读取超时设置
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);

HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (null != entity) {
result = EntityUtils.toString(entity, Consts.UTF_8);
}
} catch (Exception e) {
e.printStackTrace();
result = JSON.toJSONString(Result.error(e.getMessage()));
} finally {
httpGet.abort();
httpGet.releaseConnection();
httpClient.getConnectionManager().shutdown();
}

return result;
}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:WebSocket API 学习笔记

下一篇:Redis