HTTPClient封装Util
copy即用
package com.qin.bill.utils;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
public class HttpClientUtil {
/**
*
* Get请求
*
* @param url
*
* @param params
*
* @return
*/
public static String getForInter(String url) {
return getForInter(url, null, null);
}
/**
*
* Get请求
*
* @param url
*
* @param params
*
* @return
*/
public static String getForInter(String url, List<NameValuePair> params) {
return getForInter(url, params, null);
}
/**
*
* Get请求
*
* @param url
*
* @param params
*
* @return
*/
public static String getForInter(String url, List<NameValuePair> params,
List<NameValuePair> headerParams) {
CloseableHttpClient hc = HttpClients.createDefault();
String body = null;
try {
// Get请求
HttpGet httpget = new HttpGet(url);
// 设置参数
if (params != null && params.size() > 0) {
String str = EntityUtils.toString(new UrlEncodedFormEntity(
params));
httpget.setURI(new URI(httpget.getURI().toString() + "?" + str));
}
if (headerParams != null) {
for (NameValuePair param : headerParams) {
if (param != null && param.getName() != null
&& param.getValue() != null) {
httpget.setHeader(param.getName(), param.getValue());
}
}
}
// 发送请求
HttpResponse httpresponse = hc.execute(httpget);
// 获取返回数据
HttpEntity entity = httpresponse.getEntity();
// body = EntityUtils.toString(entity, "UTF-8");
body = new String(EntityUtils.toByteArray(entity), "UTF-8");
EntityUtils.consume(entity);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
hc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return body;
}
/**
*
* Post请求
*
* @param url
*
* @param params
*
* @return
*/
public static String post(String url) {
return post(url, null, null);
}
public static String post(String url, String sendBody) {
return post(url, sendBody, null);
}
public static String post(String url, String sendBody,
Map<String, String> headerParams) {
if (StringUtils.isBlank(sendBody)) {
sendBody = "{}";
}
CloseableHttpClient httpClient = HttpClients.createDefault();
String body = null;
try {
// Post请求
HttpPost post = new HttpPost(url);
StringEntity se = new StringEntity(sendBody, "UTF-8");
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json;charset=UTF-8"));
post.setEntity(se);
if (headerParams != null) {
for (Map.Entry<String, String> entry : headerParams.entrySet()) {
post.setHeader(entry.getKey(), entry.getValue());
}
}
// 发送请求
HttpResponse httpresponse = httpClient.execute(post);
// 获取返回数据
HttpEntity entity = httpresponse.getEntity();
body = new String(EntityUtils.toByteArray(entity), "UTF-8");
EntityUtils.consume(entity);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return body;
}
public static String putForInter(String url, String sendBody) {
return putForInter(url, sendBody, null);
}
public static String putForInter(String url, String sendBody,
List<NameValuePair> headerParams) {
CloseableHttpClient httpClient = HttpClients.createDefault();
String body = null;
try {
// Put请求
HttpPut httpput = new HttpPut(url);
StringEntity se = new StringEntity(sendBody, "UTF-8");
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json;charset=UTF-8"));
httpput.setEntity(se);
if (headerParams != null) {
for (NameValuePair param : headerParams) {
if (param != null && param.getName() != null
&& param.getValue() != null) {
httpput.setHeader(param.getName(), param.getValue());
}
}
}
// 发送请求
HttpResponse httpresponse = httpClient.execute(httpput);
// 获取返回数据
HttpEntity entity = httpresponse.getEntity();
body = new String(EntityUtils.toByteArray(entity), "UTF-8");
EntityUtils.consume(entity);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return body;
}
public static String postFile(String url,String filePath) {
CloseableHttpClient httpClient = HttpClients.createDefault();
String body = null;
try {
// Post请求
HttpPost post = new HttpPost(url);
FileEntity reqEntity = new FileEntity(new File(filePath), ContentType.MULTIPART_FORM_DATA);
// 设置请求编码
reqEntity.setContentEncoding("UTF-8");
post.setEntity(reqEntity);
// 发送请求
HttpResponse httpresponse = httpClient.execute(post);
// 获取返回数据
HttpEntity entity = httpresponse.getEntity();
body = new String(EntityUtils.toByteArray(entity), "UTF-8");
EntityUtils.consume(entity);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return body;
}
}