经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Java » 查看文章
Java HttpClient4.2.x版本get、post请求工具类
来源:cnblogs  作者:帅得无法形容²  时间:2018/11/20 12:47:28  对本文有异议

公司业务需要,跟很多公司合作,经常需要请求外部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;
}

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号