| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- package cn.com.goldenwater.dcproj.utils;
- import com.alibaba.fastjson.JSONObject;
- import org.apache.http.*;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.HttpClient;
- 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.HttpRequestBase;
- import org.apache.http.client.utils.URLEncodedUtils;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClientBuilder;
- import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
- import org.apache.http.message.BasicNameValuePair;
- import org.apache.http.protocol.HTTP;
- import org.apache.http.util.EntityUtils;
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import java.net.URISyntaxException;
- import java.nio.charset.Charset;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- /**
- * 用于处理get/post提交请求
- */
- public class HttpClientUtils {
- /**
- * httpclient读取内容时使用的字符集
- */
- public static final String CONTENT_CHARSET = "UTF-8";
- public static final Charset UTF_8 = Charset.forName("UTF-8");
- public static final Charset GBK = Charset.forName(CONTENT_CHARSET);
- /**
- * 简单get调用
- *
- * @param url
- * @param params
- * @return
- * @throws ClientProtocolException
- * @throws IOException
- * @throws URISyntaxException
- */
- public static String simpleGetInvoke(String url, Map<String, String> params)
- throws Exception {
- return simpleGetInvoke(url, params, CONTENT_CHARSET);
- }
- /**
- * 简单get调用
- *
- * @param url
- * @param params
- * @return
- * @throws ClientProtocolException
- * @throws IOException
- * @throws URISyntaxException
- */
- public static String simpleGetInvoke(String url, Map<String, String> params, String charset)
- throws ClientProtocolException, IOException, URISyntaxException {
- HttpClient client = buildHttpClient(false);
- HttpGet get = buildHttpGet(url, params);
- HttpResponse response = client.execute(get);
- assertStatus(response);
- HttpEntity entity = response.getEntity();
- if (entity != null) {
- String returnStr = EntityUtils.toString(entity, charset);
- return returnStr;
- }
- return null;
- }
- /**
- * 简单get调用
- *
- * @param url
- * @param params
- * @return
- * @throws ClientProtocolException
- * @throws IOException
- * @throws URISyntaxException
- */
- public static String simpleGetInvoke(String url, Map<String, String> params, Map<String, String> headerMap, String charset)
- throws ClientProtocolException, IOException, URISyntaxException {
- HttpClient client = buildHttpClient(false);
- HttpGet get = buildHttpGet(url, params);
- //添加header信息
- if (headerMap != null && !headerMap.isEmpty()) {
- for (String key : headerMap.keySet()) {
- get.setHeader(key, headerMap.get(key));
- }
- }
- HttpResponse response = client.execute(get);
- assertStatus(response);
- HttpEntity entity = response.getEntity();
- if (entity != null) {
- String returnStr = EntityUtils.toString(entity, charset);
- return returnStr;
- }
- return null;
- }
- /**
- * 简单post调用
- *
- * @param url
- * @param params
- * @return
- * @throws URISyntaxException
- * @throws ClientProtocolException
- * @throws IOException
- */
- public static String simplePostInvoke(String url, Map<String, String> params)
- throws Exception {
- return simplePostInvoke(url, params, CONTENT_CHARSET);
- }
- /**
- * 简单post调用
- *
- * @param url
- * @param params
- * @return
- * @throws URISyntaxException
- * @throws ClientProtocolException
- * @throws IOException
- */
- public static String simplePostInvoke(String url, Map<String, String> params, String charset)
- throws URISyntaxException, ClientProtocolException, IOException {
- HttpClient client = buildHttpClient(false);
- HttpPost postMethod = buildHttpPost(url, params);
- HttpResponse response = client.execute(postMethod);
- assertStatus(response);
- HttpEntity entity = response.getEntity();
- if (entity != null) {
- String returnStr = EntityUtils.toString(entity, charset);
- return returnStr;
- }
- return null;
- }
- /**
- * 创建HttpClient
- *
- * @param isMultiThread
- * @return
- */
- public static HttpClient buildHttpClient(boolean isMultiThread) {
- CloseableHttpClient client;
- if (isMultiThread) {
- client = HttpClientBuilder
- .create()
- .setConnectionManager(
- new PoolingHttpClientConnectionManager()).build();
- } else {
- client = HttpClientBuilder.create().build();
- // 设置代理服务器地址和端口
- }
- return client;
- }
- /**
- * 构建httpPost对象
- *
- * @param url
- * @return
- * @throws UnsupportedEncodingException
- * @throws URISyntaxException
- */
- public static HttpPost buildHttpPost(String url, Map<String, String> params)
- throws UnsupportedEncodingException, URISyntaxException {
- HttpPost post = new HttpPost(url);
- setCommonHttpMethod(post);
- HttpEntity he = null;
- if (params != null) {
- List<NameValuePair> formparams = new ArrayList<NameValuePair>();
- for (String key : params.keySet()) {
- formparams.add(new BasicNameValuePair(key, params.get(key)));
- }
- he = new UrlEncodedFormEntity(formparams, GBK);
- post.setEntity(he);
- }
- return post;
- }
- /**
- * 构建httpGet对象
- *
- * @param url
- * @return
- * @throws URISyntaxException
- */
- public static HttpGet buildHttpGet(String url, Map<String, String> params)
- throws URISyntaxException {
- HttpGet get = new HttpGet(buildGetUrl(url, params));
- return get;
- }
- /**
- * build getUrl str
- *
- * @param url
- * @param params
- * @return
- */
- private static String buildGetUrl(String url, Map<String, String> params) {
- StringBuffer uriStr = new StringBuffer(url);
- if (params != null) {
- List<NameValuePair> ps = new ArrayList<NameValuePair>();
- for (String key : params.keySet()) {
- ps.add(new BasicNameValuePair(key, params.get(key)));
- }
- uriStr.append("?");
- uriStr.append(URLEncodedUtils.format(ps, UTF_8));
- }
- return uriStr.toString();
- }
- /**
- * 设置HttpMethod通用配置
- *
- * @param httpMethod
- */
- public static void setCommonHttpMethod(HttpRequestBase httpMethod) {
- httpMethod.setHeader(HTTP.CONTENT_ENCODING, CONTENT_CHARSET);// setting
- }
- /**
- * 强验证必须是200状态否则报异常
- *
- * @param res
- * @throws HttpException
- */
- static void assertStatus(HttpResponse res) throws IOException {
- System.out.println(res.getStatusLine().getStatusCode());
- switch (res.getStatusLine().getStatusCode()) {
- case HttpStatus.SC_OK:
- break;
- default:
- throw new IOException("服务器响应状态异常,失败.");
- }
- }
- public static String doHttpsPost(String url, Map<String, String> map, Map<String, String> headerMap) {
- HttpClient httpClient = null;
- HttpPost httpPost = null;
- String result = null;
- try {
- httpClient = new SSLClient();
- httpPost = new HttpPost(url);
- //添加header信息
- if (headerMap != null && !headerMap.isEmpty()) {
- for (String key : headerMap.keySet()) {
- httpPost.setHeader(key, headerMap.get(key));
- }
- }
- //设置参数
- List<NameValuePair> list = new ArrayList<NameValuePair>();
- Iterator iterator = map.entrySet().iterator();
- while (iterator.hasNext()) {
- Map.Entry<String, String> elem = (Map.Entry<String, String>) iterator.next();
- list.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));
- }
- if (list.size() > 0) {
- UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, CONTENT_CHARSET);
- httpPost.setEntity(entity);
- }
- HttpResponse response = httpClient.execute(httpPost);
- if (response != null) {
- HttpEntity resEntity = response.getEntity();
- if (resEntity != null) {
- result = EntityUtils.toString(resEntity, CONTENT_CHARSET);
- }
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return result;
- }
- public static String doHttpsPost2(String url, Map<String, String> map, Map<String, String> headerMap) {
- HttpClient httpClient = null;
- HttpPost httpPost = null;
- String result = null;
- headerMap.put("Content-Type", "application/json;charset=utf8");
- try {
- httpClient = new SSLClient();
- httpPost = new HttpPost(url);
- //添加header信息
- if (headerMap != null && !headerMap.isEmpty()) {
- for (String key : headerMap.keySet()) {
- httpPost.setHeader(key, headerMap.get(key));
- }
- }
- //设置参数
- String jsonString = JSONObject.toJSONString(map);
- StringEntity entity = new StringEntity(jsonString, CONTENT_CHARSET);
- httpPost.setEntity(entity);
- HttpResponse response = httpClient.execute(httpPost);
- if (response != null) {
- HttpEntity resEntity = response.getEntity();
- if (resEntity != null) {
- result = EntityUtils.toString(resEntity, CONTENT_CHARSET);
- }
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return result;
- }
- }
|