以前有一个自己写的: http://www.cnblogs.com/wenbronk/p/6482706.html
后来发现一个前辈写的更好的, 再此感谢一下, 确实比我写的那个好用些
1, 创建一个HttpClientPool
package com.iwhere.easy.travel.tool; import org.apache.http.client.config.CookieSpecs; import org.apache.http.client.config.RequestConfig; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; /** * httpclient * @author chenshiyuan * */ public class HttpClientPool { private static PoolingHttpClientConnectionManager cm = null; static{ cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(400); cm.setDefaultMaxPerRoute(50); } public static CloseableHttpClient getHttpClient(){ RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).build(); CloseableHttpClient client = HttpClients.custom().setConnectionManager(cm).setDefaultRequestConfig(globalConfig).build(); return client; } }
2, 处理get或post请求的类
url: 即为请求的url
method: 为请求的方法, 在此只处理 "get" 和 "post" 方法
map: 请求参数, 如果没有则传入 new HashMap<>();
package com.iwhere.easy.travel.tool; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.apache.http.HttpEntity; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import com.alibaba.fastjson.JSONObject; public class RequestTools { public static String processHttpRequest(String url, String requestMethod, Map<String, String> paramsMap) { List<BasicNameValuePair> formparams = new ArrayList<BasicNameValuePair>(); if ("post".equals(requestMethod)) { HttpPost httppost = new HttpPost(url); httppost.setHeader("Content-Type", "application/json"); for (Iterator<String> it = paramsMap.keySet().iterator(); it.hasNext();) { String key = it.next(); String value = paramsMap.get(key); formparams.add(new BasicNameValuePair(key, value)); } return doRequest(httppost, null, formparams); } else if ("get".equals(requestMethod)) { HttpGet httppost = new HttpGet(url); for (Iterator<String> it = paramsMap.keySet().iterator(); it.hasNext();) { String key = it.next(); String value = paramsMap.get(key); formparams.add(new BasicNameValuePair(key, value)); } return doRequest(null, httppost, formparams); } return ""; } private static String doRequest(HttpPost httpPost, HttpGet httpGet, List<BasicNameValuePair> formparams) { try { CloseableHttpResponse response = null; UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(formparams); // 设置请求和传输超时时间 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(25000).setConnectTimeout(3000) .build(); if (null != httpPost) { uefEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); httpPost.setEntity(uefEntity); httpPost.setConfig(requestConfig); response = HttpClientPool.getHttpClient().execute(httpPost); } else { httpGet.setConfig(requestConfig); response = HttpClientPool.getHttpClient().execute(httpGet); } HttpEntity entity = response.getEntity(); String str = EntityUtils.toString(entity, "UTF-8"); if (null == str || "".equals(str)) { return ""; } else { return str; } } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { } return ""; }
}
3, 后来发现这个没法处理json格式的body, 所以写了个json格式的post请求方法
/** * 处理json格式的body post请求 * * @return * @throws Exception * @throws ClientProtocolException */ public static String processPostJson(String postUrl, JSONObject jsonObj) throws ClientProtocolException, Exception { // HttpClient httpclient = new DefaultHttpClient(); HttpPost post = new HttpPost(postUrl); post.setHeader("Content-Type", "application/json"); post.addHeader("Authorization", "Basic YWRtaW46"); String str = null; StringEntity s = new StringEntity(jsonObj.toJSONString(), "utf-8"); s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(25000).setConnectTimeout(3000).build(); post.setEntity(s); post.setConfig(requestConfig); CloseableHttpResponse response = HttpClientPool.getHttpClient().execute(post); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instreams = entity.getContent(); str = convertStreamToString(instreams); post.abort(); } // System.out.println(str); return str; } private static String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line + " "); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); }