c935a443861ec0ff9659de675cfe58356f79740a.svn-base 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. package cn.com.goldenwater.dcproj.utils;
  2. import com.alibaba.fastjson.JSONObject;
  3. import org.apache.http.*;
  4. import org.apache.http.client.ClientProtocolException;
  5. import org.apache.http.client.HttpClient;
  6. import org.apache.http.client.entity.UrlEncodedFormEntity;
  7. import org.apache.http.client.methods.HttpGet;
  8. import org.apache.http.client.methods.HttpPost;
  9. import org.apache.http.client.methods.HttpRequestBase;
  10. import org.apache.http.client.utils.URLEncodedUtils;
  11. import org.apache.http.entity.StringEntity;
  12. import org.apache.http.impl.client.CloseableHttpClient;
  13. import org.apache.http.impl.client.HttpClientBuilder;
  14. import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
  15. import org.apache.http.message.BasicNameValuePair;
  16. import org.apache.http.protocol.HTTP;
  17. import org.apache.http.util.EntityUtils;
  18. import java.io.IOException;
  19. import java.io.UnsupportedEncodingException;
  20. import java.net.URISyntaxException;
  21. import java.nio.charset.Charset;
  22. import java.util.ArrayList;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import java.util.Map;
  26. /**
  27. * 用于处理get/post提交请求
  28. */
  29. public class HttpClientUtils {
  30. /**
  31. * httpclient读取内容时使用的字符集
  32. */
  33. public static final String CONTENT_CHARSET = "UTF-8";
  34. public static final Charset UTF_8 = Charset.forName("UTF-8");
  35. public static final Charset GBK = Charset.forName(CONTENT_CHARSET);
  36. /**
  37. * 简单get调用
  38. *
  39. * @param url
  40. * @param params
  41. * @return
  42. * @throws ClientProtocolException
  43. * @throws IOException
  44. * @throws URISyntaxException
  45. */
  46. public static String simpleGetInvoke(String url, Map<String, String> params)
  47. throws Exception {
  48. return simpleGetInvoke(url, params, CONTENT_CHARSET);
  49. }
  50. /**
  51. * 简单get调用
  52. *
  53. * @param url
  54. * @param params
  55. * @return
  56. * @throws ClientProtocolException
  57. * @throws IOException
  58. * @throws URISyntaxException
  59. */
  60. public static String simpleGetInvoke(String url, Map<String, String> params, String charset)
  61. throws ClientProtocolException, IOException, URISyntaxException {
  62. HttpClient client = buildHttpClient(false);
  63. HttpGet get = buildHttpGet(url, params);
  64. HttpResponse response = client.execute(get);
  65. assertStatus(response);
  66. HttpEntity entity = response.getEntity();
  67. if (entity != null) {
  68. String returnStr = EntityUtils.toString(entity, charset);
  69. return returnStr;
  70. }
  71. return null;
  72. }
  73. /**
  74. * 简单get调用
  75. *
  76. * @param url
  77. * @param params
  78. * @return
  79. * @throws ClientProtocolException
  80. * @throws IOException
  81. * @throws URISyntaxException
  82. */
  83. public static String simpleGetInvoke(String url, Map<String, String> params, Map<String, String> headerMap, String charset)
  84. throws ClientProtocolException, IOException, URISyntaxException {
  85. HttpClient client = buildHttpClient(false);
  86. HttpGet get = buildHttpGet(url, params);
  87. //添加header信息
  88. if (headerMap != null && !headerMap.isEmpty()) {
  89. for (String key : headerMap.keySet()) {
  90. get.setHeader(key, headerMap.get(key));
  91. }
  92. }
  93. HttpResponse response = client.execute(get);
  94. assertStatus(response);
  95. HttpEntity entity = response.getEntity();
  96. if (entity != null) {
  97. String returnStr = EntityUtils.toString(entity, charset);
  98. return returnStr;
  99. }
  100. return null;
  101. }
  102. /**
  103. * 简单post调用
  104. *
  105. * @param url
  106. * @param params
  107. * @return
  108. * @throws URISyntaxException
  109. * @throws ClientProtocolException
  110. * @throws IOException
  111. */
  112. public static String simplePostInvoke(String url, Map<String, String> params)
  113. throws Exception {
  114. return simplePostInvoke(url, params, CONTENT_CHARSET);
  115. }
  116. /**
  117. * 简单post调用
  118. *
  119. * @param url
  120. * @param params
  121. * @return
  122. * @throws URISyntaxException
  123. * @throws ClientProtocolException
  124. * @throws IOException
  125. */
  126. public static String simplePostInvoke(String url, Map<String, String> params, String charset)
  127. throws URISyntaxException, ClientProtocolException, IOException {
  128. HttpClient client = buildHttpClient(false);
  129. HttpPost postMethod = buildHttpPost(url, params);
  130. HttpResponse response = client.execute(postMethod);
  131. assertStatus(response);
  132. HttpEntity entity = response.getEntity();
  133. if (entity != null) {
  134. String returnStr = EntityUtils.toString(entity, charset);
  135. return returnStr;
  136. }
  137. return null;
  138. }
  139. /**
  140. * 创建HttpClient
  141. *
  142. * @param isMultiThread
  143. * @return
  144. */
  145. public static HttpClient buildHttpClient(boolean isMultiThread) {
  146. CloseableHttpClient client;
  147. if (isMultiThread) {
  148. client = HttpClientBuilder
  149. .create()
  150. .setConnectionManager(
  151. new PoolingHttpClientConnectionManager()).build();
  152. } else {
  153. client = HttpClientBuilder.create().build();
  154. // 设置代理服务器地址和端口
  155. }
  156. return client;
  157. }
  158. /**
  159. * 构建httpPost对象
  160. *
  161. * @param url
  162. * @return
  163. * @throws UnsupportedEncodingException
  164. * @throws URISyntaxException
  165. */
  166. public static HttpPost buildHttpPost(String url, Map<String, String> params)
  167. throws UnsupportedEncodingException, URISyntaxException {
  168. HttpPost post = new HttpPost(url);
  169. setCommonHttpMethod(post);
  170. HttpEntity he = null;
  171. if (params != null) {
  172. List<NameValuePair> formparams = new ArrayList<NameValuePair>();
  173. for (String key : params.keySet()) {
  174. formparams.add(new BasicNameValuePair(key, params.get(key)));
  175. }
  176. he = new UrlEncodedFormEntity(formparams, GBK);
  177. post.setEntity(he);
  178. }
  179. return post;
  180. }
  181. /**
  182. * 构建httpGet对象
  183. *
  184. * @param url
  185. * @return
  186. * @throws URISyntaxException
  187. */
  188. public static HttpGet buildHttpGet(String url, Map<String, String> params)
  189. throws URISyntaxException {
  190. HttpGet get = new HttpGet(buildGetUrl(url, params));
  191. return get;
  192. }
  193. /**
  194. * build getUrl str
  195. *
  196. * @param url
  197. * @param params
  198. * @return
  199. */
  200. private static String buildGetUrl(String url, Map<String, String> params) {
  201. StringBuffer uriStr = new StringBuffer(url);
  202. if (params != null) {
  203. List<NameValuePair> ps = new ArrayList<NameValuePair>();
  204. for (String key : params.keySet()) {
  205. ps.add(new BasicNameValuePair(key, params.get(key)));
  206. }
  207. uriStr.append("?");
  208. uriStr.append(URLEncodedUtils.format(ps, UTF_8));
  209. }
  210. return uriStr.toString();
  211. }
  212. /**
  213. * 设置HttpMethod通用配置
  214. *
  215. * @param httpMethod
  216. */
  217. public static void setCommonHttpMethod(HttpRequestBase httpMethod) {
  218. httpMethod.setHeader(HTTP.CONTENT_ENCODING, CONTENT_CHARSET);// setting
  219. }
  220. /**
  221. * 强验证必须是200状态否则报异常
  222. *
  223. * @param res
  224. * @throws HttpException
  225. */
  226. static void assertStatus(HttpResponse res) throws IOException {
  227. System.out.println(res.getStatusLine().getStatusCode());
  228. switch (res.getStatusLine().getStatusCode()) {
  229. case HttpStatus.SC_OK:
  230. break;
  231. default:
  232. throw new IOException("服务器响应状态异常,失败.");
  233. }
  234. }
  235. public static String doHttpsPost(String url, Map<String, String> map, Map<String, String> headerMap) {
  236. HttpClient httpClient = null;
  237. HttpPost httpPost = null;
  238. String result = null;
  239. try {
  240. httpClient = new SSLClient();
  241. httpPost = new HttpPost(url);
  242. //添加header信息
  243. if (headerMap != null && !headerMap.isEmpty()) {
  244. for (String key : headerMap.keySet()) {
  245. httpPost.setHeader(key, headerMap.get(key));
  246. }
  247. }
  248. //设置参数
  249. List<NameValuePair> list = new ArrayList<NameValuePair>();
  250. Iterator iterator = map.entrySet().iterator();
  251. while (iterator.hasNext()) {
  252. Map.Entry<String, String> elem = (Map.Entry<String, String>) iterator.next();
  253. list.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));
  254. }
  255. if (list.size() > 0) {
  256. UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, CONTENT_CHARSET);
  257. httpPost.setEntity(entity);
  258. }
  259. HttpResponse response = httpClient.execute(httpPost);
  260. if (response != null) {
  261. HttpEntity resEntity = response.getEntity();
  262. if (resEntity != null) {
  263. result = EntityUtils.toString(resEntity, CONTENT_CHARSET);
  264. }
  265. }
  266. } catch (Exception ex) {
  267. ex.printStackTrace();
  268. }
  269. return result;
  270. }
  271. public static String doHttpsPost2(String url, Map<String, String> map, Map<String, String> headerMap) {
  272. HttpClient httpClient = null;
  273. HttpPost httpPost = null;
  274. String result = null;
  275. headerMap.put("Content-Type", "application/json;charset=utf8");
  276. try {
  277. httpClient = new SSLClient();
  278. httpPost = new HttpPost(url);
  279. //添加header信息
  280. if (headerMap != null && !headerMap.isEmpty()) {
  281. for (String key : headerMap.keySet()) {
  282. httpPost.setHeader(key, headerMap.get(key));
  283. }
  284. }
  285. //设置参数
  286. String jsonString = JSONObject.toJSONString(map);
  287. StringEntity entity = new StringEntity(jsonString, CONTENT_CHARSET);
  288. httpPost.setEntity(entity);
  289. HttpResponse response = httpClient.execute(httpPost);
  290. if (response != null) {
  291. HttpEntity resEntity = response.getEntity();
  292. if (resEntity != null) {
  293. result = EntityUtils.toString(resEntity, CONTENT_CHARSET);
  294. }
  295. }
  296. } catch (Exception ex) {
  297. ex.printStackTrace();
  298. }
  299. return result;
  300. }
  301. }