HttpClientUitl工具类

public class HttpClient {

private CloseableHttpClient httpClient;

public HttpClient() {
this.httpClient = HttpClients.createDefault();
}

/**
* 带参数的get请求
*
* @param url
* @param map
* @return
* @throws Exception
*/
public HttpResult doGet(String url, Map<String, Object> map) throws Exception {
URIBuilder uriBuilder = new URIBuilder(url);
if (map != null) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
uriBuilder.setParameter(entry.getKey(), entry.getValue().toString());
}
}
HttpGet httpGet = new HttpGet(uriBuilder.build());
CloseableHttpResponse response = this.httpClient.execute(httpGet);
HttpResult httpResult = null;
if (response.getEntity() != null) {
httpResult = new HttpResult(response.getStatusLine().getStatusCode(),
EntityUtils.toString(response.getEntity(), "UTF-8"));
} else {
httpResult = new HttpResult(response.getStatusLine().getStatusCode(), "");
}
return httpResult;
}

/**
* 不带参数的get请求
*
* @param url
* @return
* @throws Exception
*/
public HttpResult doGet(String url) throws Exception {
HttpResult httpResult = this.doGet(url, null);
return httpResult;
}

/**
* 带参数的post请求
*
* @param url
* @param map
* @return
* @throws Exception
*/
public HttpResult doPost(String url, Map<String, Object> map) throws Exception {
HttpPost httpPost = new HttpPost(url);
if (map != null) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
}
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, "UTF-8");
httpPost.setEntity(formEntity);
}
CloseableHttpResponse response = this.httpClient.execute(httpPost);
HttpResult httpResult = null;
if (response.getEntity() != null) {
httpResult = new HttpResult(response.getStatusLine().getStatusCode(),
EntityUtils.toString(response.getEntity(), "UTF-8"));
} else {
httpResult = new HttpResult(response.getStatusLine().getStatusCode(), "");
}
return httpResult;
}

/**
* 不带参数的post请求
*
* @param url
* @return
* @throws Exception
*/
public HttpResult doPost(String url) throws Exception {
HttpResult httpResult = this.doPost(url, null);
return httpResult;
}

/**
* 带参数的Put请求
*
* @param url
* @param map
* @return
* @throws Exception
*/
public HttpResult doPut(String url, Map<String, Object> map) throws Exception {
HttpPut httpPut = new HttpPut(url);
if (map != null) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
}
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, "UTF-8");
httpPut.setEntity(formEntity);
}
CloseableHttpResponse response = this.httpClient.execute(httpPut);
HttpResult httpResult = null;
if (response.getEntity() != null) {
httpResult = new HttpResult(response.getStatusLine().getStatusCode(),
EntityUtils.toString(response.getEntity(), "UTF-8"));
} else {
httpResult = new HttpResult(response.getStatusLine().getStatusCode(), "");
}
return httpResult;
}

/**
* 带参数的Delete请求
*
* @param url
* @param map
* @return
* @throws Exception
*/
public HttpResult doDelete(String url, Map<String, Object> map) throws Exception {
URIBuilder uriBuilder = new URIBuilder(url);
if (map != null) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
uriBuilder.setParameter(entry.getKey(), entry.getValue().toString());
}
}
HttpDelete httpDelete = new HttpDelete(uriBuilder.build());
CloseableHttpResponse response = this.httpClient.execute(httpDelete);
HttpResult httpResult = null;
if (response.getEntity() != null) {
httpResult = new HttpResult(response.getStatusLine().getStatusCode(),
EntityUtils.toString(response.getEntity(), "UTF-8"));
} else {
httpResult = new HttpResult(response.getStatusLine().getStatusCode(), "");
}
return httpResult;
}

/**
* post请求(用于请求json格式的参数)
* @param url
* @param params
* @return
*/
public static String doPostJson(String url, String params) throws Exception {

CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);// 创建httpPost
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-Type", "application/json");
String charSet = "UTF-8";
StringEntity entity = new StringEntity(params, charSet);
httpPost.setEntity(entity);
CloseableHttpResponse response = null;

try {

response = httpclient.execute(httpPost);
StatusLine status = response.getStatusLine();
int state = status.getStatusCode();
if (state == HttpStatus.SC_OK) {
HttpEntity responseEntity = response.getEntity();
String jsonString = EntityUtils.toString(responseEntity);
return jsonString;
}
else{
logger.error("请求返回:"+state+"("+url+")");
}
}
finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}

}
原文地址:https://www.cnblogs.com/h-c-g/p/9978849.html