Java HttpClient4.2.x版本getpost请求工具类

Posted james-i71

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java HttpClient4.2.x版本getpost请求工具类相关的知识,希望对你有一定的参考价值。

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








































































































































以上是关于Java HttpClient4.2.x版本getpost请求工具类的主要内容,如果未能解决你的问题,请参考以下文章

hbase java api样例(版本1.3.1,新API)

Java学习之HttpClient的GET与POST请求

Spring 中 Swagger 2 GET请求注解

在ubuntu下用apt-get安装和管理java

[Java安全]Fastjson>=1.2.36$ref引用可触发get方法分析

[Java安全]Fastjson>=1.2.36$ref引用可触发get方法分析