httpclient4.3发送post和get请求的工具代码。
public static final String ISO88591 = "ISO-8859-1";
public static final String GBK = "GBK";
public static final String UTF8 = "UTF-8";
/**
* @throws IOException
*
* @Title: httpPost
* @Description: httpPost请求工具类
* @param url
* @param params
* @param charset
* @param socketTimeOut
* @param connectTimeOut
* @param connReqTimeOut
* @return 设定文件
* @return String 返回类型
* @date 2015年7月10日 上午11:55:39
* @throws
*/
public static String httpPost(String url, StringEntity params, Map<String, String> headerMap, String charset, SSLConnectionSocketFactory sslsf, int socketTimeOut, int connectTimeOut, int connReqTimeOut ) throws IOException{
if (Tools.checkNull(charset)) {
charset = UTF8;
}
HttpPost httppost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeOut).setConnectTimeout(connectTimeOut).setConnectionRequestTimeout(connReqTimeOut)
.build();//设置请求和传输超时时间
httppost.setConfig(requestConfig);
if (!Tools.checkNull(headerMap) && !headerMap.isEmpty()) {
for (Entry<String, String> header : headerMap.entrySet()) {
if (!Tools.checkNull(header.getKey()) && !Tools.checkNull(header.getValue())) {
httppost.setHeader(header.getKey(), header.getValue());
}
}
}
String result = null;
HttpClientBuilder httpbuilder = HttpClients.custom();
if (!Tools.checkNull(sslsf)) {//如果证书不为空则加载证书
httpbuilder.setSSLSocketFactory(sslsf);
}
CloseableHttpClient httpclient = httpbuilder.build();
CloseableHttpResponse response = null;
try{
httppost.setEntity(params);
response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
if (entity != null) {
return EntityUtils.toString(entity, charset);
}
} else {
throw new BaseException(ExceptionEnu.HTTP_ERROR);
}
} finally{
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
* @throws IOException
*
* @Title: httpPost
* @Description: httpPost请求工具类
* @param url
* @param params
* @param timeOut
* @param charset
* @return 设定文件
* @return String 返回类型
* @date 2015年7月2日 下午4:51:55
* @throws
*/
public static String httpPost(String url, Map<String, Object> params, String charset, SSLConnectionSocketFactory sslsf, int timeOut) throws IOException{
//定义传递参数
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
if (params != null) {
for (Entry<String,Object> entry : params.entrySet()) {
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()==null?"":entry.getValue().toString()));
}
}
UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(formparams, charset);//对参数进行编码 ;
return httpPost(url, uefEntity, null, charset, sslsf, timeOut, timeOut, timeOut);
}
/**
* @throws IOException
*
* @Title: httpPost
* @Description: httpPost请求工具类
* @param url
* @param params
* @param timeOut
* @param charset
* @return 设定文件
* @return String 返回类型
* @date 2015年7月2日 下午4:51:55
* @throws
*/
public static String httpPost(String url, Map<String, Object> params, Map<String, String> header, String charset, SSLConnectionSocketFactory sslsf, int timeOut) throws IOException{
//定义传递参数
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
if (params != null) {
for (Entry<String,Object> entry : params.entrySet()) {
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()==null?"":entry.getValue().toString()));
}
}
if (Tools.checkNull(charset)) {
charset = UTF8;
}
UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(formparams, charset);//对参数进行编码 ;
return httpPost(url, uefEntity, header, charset, sslsf, timeOut, timeOut, timeOut);
}
/**
* 发送json请求
* @param url
* @param params
* @param charset
* @param timeOut
* @return
* @throws IOException
*/
public static String httpJsonPost(String url, String jsonParams, String charset, SSLConnectionSocketFactory sslsf, int timeOut) throws IOException{
charset = Tools.checkNull(charset) ? "utf-8" : charset;
StringEntity params = new StringEntity(jsonParams, charset );
return httpPost(url, params, null, charset, sslsf, timeOut, timeOut, timeOut);
}
/**
* @throws IOException
*
* @Title: httpGet
* @Description: httpPost请求工具类
* @param url
* @param params
* @param charset
* @param socketTimeOut
* @param connectTimeOut
* @param connReqTimeOut
* @return 设定文件
* @return String 返回类型
* @date 2015年7月10日 上午11:55:39
* @throws
*/
public static String httpGet(String url, Map<String, Object> params, String charset, int socketTimeOut, int connectTimeOut, int connReqTimeOut ) throws IOException{
if (Tools.checkNull(charset)) {
charset = UTF8;
}
String result = null;
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
try{
//定义传递参数
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
for (Entry<String,Object> entry : params.entrySet()) {
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()==null?"":entry.getValue().toString()));
}
url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(formparams, charset), charset);
log.info("url : [{}]", url);
HttpGet httpget = new HttpGet(url);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeOut).setConnectTimeout(connectTimeOut).setConnectionRequestTimeout(connReqTimeOut)
.build();//设置请求和传输超时时间
httpget.setConfig(requestConfig);
response = httpclient.execute(httpget);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
if (entity != null) {
return EntityUtils.toString(entity, charset);
}
} else {
throw new BaseException(ExceptionEnu.HTTP_ERROR);
}
} finally{
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
* @throws IOException
*
* @Title: httpGet
* @Description: httpGet请求工具类
* @param url
* @param params
* @param charset
* @param timeOut
* @return 设定文件
* @return String 返回类型
* @date 2015年7月15日 上午10:34:41
* @throws
*/
public static String httpGet(String url, Map<String, Object> params, String charset, int timeOut) throws IOException{
return httpGet(url, params, charset, timeOut, timeOut, timeOut);
}
/**
* @throws IOException
*
* @Title: httpGetBytes
* @param url
* @param params
* @param charset
* @param socketTimeOut
* @param connectTimeOut
* @param connReqTimeOut
* @return byte[] 返回类型
* @date 2015年7月10日 上午11:55:39
* @throws
*/
public static byte[] httpGetBytes(String url, Map<String, Object> params, String charset, int socketTimeOut, int connectTimeOut, int connReqTimeOut ) throws IOException{
if (Tools.checkNull(charset)) {
charset = UTF8;
}
byte[] result = null;
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
try{
if (params != null && !params.isEmpty()) {
//定义传递参数
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
for (Entry<String,Object> entry : params.entrySet()) {
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()==null?"":entry.getValue().toString()));
}
url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(formparams, charset), charset);
}
log.info("url : [{}]", url);
HttpGet httpget = new HttpGet(url);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeOut).setConnectTimeout(connectTimeOut).setConnectionRequestTimeout(connReqTimeOut)
.build();//设置请求和传输超时时间
httpget.setConfig(requestConfig);
response = httpclient.execute(httpget);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
if (entity != null) {
return EntityUtils.toByteArray(entity);
}
} else {
throw new BaseException(ExceptionEnu.HTTP_ERROR);
}
} finally{
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
* @throws IOException
*
* @Title: httpGetBytes
* @param url
* @param params
* @param charset
* @param timeOut
* @return byte[] 返回类型
* @date 2015年7月10日 上午11:55:39
* @throws
*/
public static byte[] httpGetBytes(String url, Map<String, Object> params, String charset, int timeOut) throws IOException{
return httpGetBytes(url, params, charset, timeOut, timeOut, timeOut);
}