HttpUtils.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. package com.caesolver.server.util;
  2. import org.apache.commons.lang.StringUtils;
  3. import org.apache.http.HttpResponse;
  4. import org.apache.http.NameValuePair;
  5. import org.apache.http.client.HttpClient;
  6. import org.apache.http.client.entity.UrlEncodedFormEntity;
  7. import org.apache.http.client.methods.HttpDelete;
  8. import org.apache.http.client.methods.HttpGet;
  9. import org.apache.http.client.methods.HttpPost;
  10. import org.apache.http.client.methods.HttpPut;
  11. import org.apache.http.conn.ClientConnectionManager;
  12. import org.apache.http.conn.scheme.Scheme;
  13. import org.apache.http.conn.scheme.SchemeRegistry;
  14. import org.apache.http.conn.ssl.SSLSocketFactory;
  15. import org.apache.http.entity.ByteArrayEntity;
  16. import org.apache.http.entity.StringEntity;
  17. import org.apache.http.impl.client.DefaultHttpClient;
  18. import org.apache.http.message.BasicNameValuePair;
  19. import javax.net.ssl.SSLContext;
  20. import javax.net.ssl.TrustManager;
  21. import javax.net.ssl.X509TrustManager;
  22. import java.io.UnsupportedEncodingException;
  23. import java.net.URLEncoder;
  24. import java.security.KeyManagementException;
  25. import java.security.NoSuchAlgorithmException;
  26. import java.security.cert.X509Certificate;
  27. import java.util.ArrayList;
  28. import java.util.List;
  29. import java.util.Map;
  30. public class HttpUtils {
  31. /**
  32. * get
  33. *
  34. * @param host
  35. * @param path
  36. * @param method
  37. * @param headers
  38. * @param querys
  39. * @return
  40. * @throws Exception
  41. */
  42. public static HttpResponse doGet(String host, String path, String method,
  43. Map<String, String> headers,
  44. Map<String, String> querys)
  45. throws Exception {
  46. HttpClient httpClient = wrapClient(host);
  47. HttpGet request = new HttpGet(buildUrl(host, path, querys));
  48. for (Map.Entry<String, String> e : headers.entrySet()) {
  49. request.addHeader(e.getKey(), e.getValue());
  50. }
  51. return httpClient.execute(request);
  52. }
  53. /**
  54. * post form
  55. *
  56. * @param host
  57. * @param path
  58. * @param method
  59. * @param headers
  60. * @param querys
  61. * @param bodys
  62. * @return
  63. * @throws Exception
  64. */
  65. public static HttpResponse doPost(String host, String path, String method,
  66. Map<String, String> headers,
  67. Map<String, String> querys,
  68. Map<String, String> bodys)
  69. throws Exception {
  70. HttpClient httpClient = wrapClient(host);
  71. HttpPost request = new HttpPost(buildUrl(host, path, querys));
  72. for (Map.Entry<String, String> e : headers.entrySet()) {
  73. request.addHeader(e.getKey(), e.getValue());
  74. }
  75. if (bodys != null) {
  76. List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
  77. for (String key : bodys.keySet()) {
  78. nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));
  79. }
  80. UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8");
  81. formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
  82. request.setEntity(formEntity);
  83. }
  84. return httpClient.execute(request);
  85. }
  86. /**
  87. * Post String
  88. *
  89. * @param host
  90. * @param path
  91. * @param method
  92. * @param headers
  93. * @param querys
  94. * @param body
  95. * @return
  96. * @throws Exception
  97. */
  98. public static HttpResponse doPost(String host, String path, String method,
  99. Map<String, String> headers,
  100. Map<String, String> querys,
  101. String body)
  102. throws Exception {
  103. HttpClient httpClient = wrapClient(host);
  104. HttpPost request = new HttpPost(buildUrl(host, path, querys));
  105. for (Map.Entry<String, String> e : headers.entrySet()) {
  106. request.addHeader(e.getKey(), e.getValue());
  107. }
  108. if (StringUtils.isNotBlank(body)) {
  109. request.setEntity(new StringEntity(body, "utf-8"));
  110. }
  111. return httpClient.execute(request);
  112. }
  113. /**
  114. * Post stream
  115. *
  116. * @param host
  117. * @param path
  118. * @param method
  119. * @param headers
  120. * @param querys
  121. * @param body
  122. * @return
  123. * @throws Exception
  124. */
  125. public static HttpResponse doPost(String host, String path, String method,
  126. Map<String, String> headers,
  127. Map<String, String> querys,
  128. byte[] body)
  129. throws Exception {
  130. HttpClient httpClient = wrapClient(host);
  131. HttpPost request = new HttpPost(buildUrl(host, path, querys));
  132. for (Map.Entry<String, String> e : headers.entrySet()) {
  133. request.addHeader(e.getKey(), e.getValue());
  134. }
  135. if (body != null) {
  136. request.setEntity(new ByteArrayEntity(body));
  137. }
  138. return httpClient.execute(request);
  139. }
  140. /**
  141. * Put String
  142. * @param host
  143. * @param path
  144. * @param method
  145. * @param headers
  146. * @param querys
  147. * @param body
  148. * @return
  149. * @throws Exception
  150. */
  151. public static HttpResponse doPut(String host, String path, String method,
  152. Map<String, String> headers,
  153. Map<String, String> querys,
  154. String body)
  155. throws Exception {
  156. HttpClient httpClient = wrapClient(host);
  157. HttpPut request = new HttpPut(buildUrl(host, path, querys));
  158. for (Map.Entry<String, String> e : headers.entrySet()) {
  159. request.addHeader(e.getKey(), e.getValue());
  160. }
  161. if (StringUtils.isNotBlank(body)) {
  162. request.setEntity(new StringEntity(body, "utf-8"));
  163. }
  164. return httpClient.execute(request);
  165. }
  166. /**
  167. * Put stream
  168. * @param host
  169. * @param path
  170. * @param method
  171. * @param headers
  172. * @param querys
  173. * @param body
  174. * @return
  175. * @throws Exception
  176. */
  177. public static HttpResponse doPut(String host, String path, String method,
  178. Map<String, String> headers,
  179. Map<String, String> querys,
  180. byte[] body)
  181. throws Exception {
  182. HttpClient httpClient = wrapClient(host);
  183. HttpPut request = new HttpPut(buildUrl(host, path, querys));
  184. for (Map.Entry<String, String> e : headers.entrySet()) {
  185. request.addHeader(e.getKey(), e.getValue());
  186. }
  187. if (body != null) {
  188. request.setEntity(new ByteArrayEntity(body));
  189. }
  190. return httpClient.execute(request);
  191. }
  192. /**
  193. * Delete
  194. *
  195. * @param host
  196. * @param path
  197. * @param method
  198. * @param headers
  199. * @param querys
  200. * @return
  201. * @throws Exception
  202. */
  203. public static HttpResponse doDelete(String host, String path, String method,
  204. Map<String, String> headers,
  205. Map<String, String> querys)
  206. throws Exception {
  207. HttpClient httpClient = wrapClient(host);
  208. HttpDelete request = new HttpDelete(buildUrl(host, path, querys));
  209. for (Map.Entry<String, String> e : headers.entrySet()) {
  210. request.addHeader(e.getKey(), e.getValue());
  211. }
  212. return httpClient.execute(request);
  213. }
  214. private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException {
  215. StringBuilder sbUrl = new StringBuilder();
  216. sbUrl.append(host);
  217. if (!StringUtils.isBlank(path)) {
  218. sbUrl.append(path);
  219. }
  220. if (null != querys) {
  221. StringBuilder sbQuery = new StringBuilder();
  222. for (Map.Entry<String, String> query : querys.entrySet()) {
  223. if (0 < sbQuery.length()) {
  224. sbQuery.append("&");
  225. }
  226. if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {
  227. sbQuery.append(query.getValue());
  228. }
  229. if (!StringUtils.isBlank(query.getKey())) {
  230. sbQuery.append(query.getKey());
  231. if (!StringUtils.isBlank(query.getValue())) {
  232. sbQuery.append("=");
  233. sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));
  234. }
  235. }
  236. }
  237. if (0 < sbQuery.length()) {
  238. sbUrl.append("?").append(sbQuery);
  239. }
  240. }
  241. return sbUrl.toString();
  242. }
  243. private static HttpClient wrapClient(String host) {
  244. HttpClient httpClient = new DefaultHttpClient();
  245. if (host.startsWith("https://")) {
  246. sslClient(httpClient);
  247. }
  248. return httpClient;
  249. }
  250. private static void sslClient(HttpClient httpClient) {
  251. try {
  252. SSLContext ctx = SSLContext.getInstance("TLS");
  253. X509TrustManager tm = new X509TrustManager() {
  254. public X509Certificate[] getAcceptedIssuers() {
  255. return null;
  256. }
  257. public void checkClientTrusted(X509Certificate[] xcs, String str) {
  258. }
  259. public void checkServerTrusted(X509Certificate[] xcs, String str) {
  260. }
  261. };
  262. ctx.init(null, new TrustManager[] { tm }, null);
  263. SSLSocketFactory ssf = new SSLSocketFactory(ctx);
  264. ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
  265. ClientConnectionManager ccm = httpClient.getConnectionManager();
  266. SchemeRegistry registry = ccm.getSchemeRegistry();
  267. registry.register(new Scheme("https", 443, ssf));
  268. } catch (KeyManagementException ex) {
  269. throw new RuntimeException(ex);
  270. } catch (NoSuchAlgorithmException ex) {
  271. throw new RuntimeException(ex);
  272. }
  273. }
  274. }