HttpClient.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package com.zhentao.utils;
  2. import org.apache.http.Consts;
  3. import org.apache.http.HttpEntity;
  4. import org.apache.http.NameValuePair;
  5. import org.apache.http.client.ClientProtocolException;
  6. import org.apache.http.client.entity.UrlEncodedFormEntity;
  7. import org.apache.http.client.methods.*;
  8. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  9. import org.apache.http.conn.ssl.SSLContextBuilder;
  10. import org.apache.http.conn.ssl.TrustStrategy;
  11. import org.apache.http.entity.StringEntity;
  12. import org.apache.http.impl.client.CloseableHttpClient;
  13. import org.apache.http.impl.client.HttpClients;
  14. import org.apache.http.message.BasicNameValuePair;
  15. import org.apache.http.util.EntityUtils;
  16. import javax.net.ssl.SSLContext;
  17. import java.io.IOException;
  18. import java.security.cert.CertificateException;
  19. import java.security.cert.X509Certificate;
  20. import java.text.ParseException;
  21. import java.util.HashMap;
  22. import java.util.LinkedList;
  23. import java.util.List;
  24. import java.util.Map;
  25. /**
  26. * http请求客户端
  27. *
  28. * @author Administrator
  29. *
  30. */
  31. public class HttpClient {
  32. private String url;
  33. private Map<String, String> param;
  34. private int statusCode;
  35. private String content;
  36. private String xmlParam;
  37. private boolean isHttps;
  38. public boolean isHttps() {
  39. return isHttps;
  40. }
  41. public void setHttps(boolean isHttps) {
  42. this.isHttps = isHttps;
  43. }
  44. public String getXmlParam() {
  45. return xmlParam;
  46. }
  47. public void setXmlParam(String xmlParam) {
  48. this.xmlParam = xmlParam;
  49. }
  50. public HttpClient(String url, Map<String, String> param) {
  51. this.url = url;
  52. this.param = param;
  53. }
  54. public HttpClient(String url) {
  55. this.url = url;
  56. }
  57. public void setParameter(Map<String, String> map) {
  58. param = map;
  59. }
  60. public void addParameter(String key, String value) {
  61. if (param == null)
  62. param = new HashMap<String, String>();
  63. param.put(key, value);
  64. }
  65. public void post() throws ClientProtocolException, IOException {
  66. HttpPost http = new HttpPost(url);
  67. setEntity(http);
  68. execute(http);
  69. }
  70. public void put() throws ClientProtocolException, IOException {
  71. HttpPut http = new HttpPut(url);
  72. setEntity(http);
  73. execute(http);
  74. }
  75. public void get() throws ClientProtocolException, IOException {
  76. if (param != null) {
  77. StringBuilder url = new StringBuilder(this.url);
  78. boolean isFirst = true;
  79. for (String key : param.keySet()) {
  80. if (isFirst)
  81. url.append("?");
  82. else
  83. url.append("&");
  84. url.append(key).append("=").append(param.get(key));
  85. }
  86. this.url = url.toString();
  87. }
  88. HttpGet http = new HttpGet(url);
  89. execute(http);
  90. }
  91. /**
  92. * set http post,put param
  93. */
  94. private void setEntity(HttpEntityEnclosingRequestBase http) {
  95. if (param != null) {
  96. List<NameValuePair> nvps = new LinkedList<NameValuePair>();
  97. for (String key : param.keySet())
  98. nvps.add(new BasicNameValuePair(key, param.get(key))); // 参数
  99. http.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8)); // 设置参数
  100. }
  101. if (xmlParam != null) {
  102. http.setEntity(new StringEntity(xmlParam, Consts.UTF_8));
  103. }
  104. }
  105. private void execute(HttpUriRequest http) throws ClientProtocolException,
  106. IOException {
  107. CloseableHttpClient httpClient = null;
  108. try {
  109. if (isHttps) {
  110. SSLContext sslContext = new SSLContextBuilder()
  111. .loadTrustMaterial(null, new TrustStrategy() {
  112. // 信任所有
  113. public boolean isTrusted(X509Certificate[] chain,
  114. String authType)
  115. throws CertificateException {
  116. return true;
  117. }
  118. }).build();
  119. SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
  120. sslContext);
  121. httpClient = HttpClients.custom().setSSLSocketFactory(sslsf)
  122. .build();
  123. } else {
  124. httpClient = HttpClients.createDefault();
  125. }
  126. CloseableHttpResponse response = httpClient.execute(http);
  127. try {
  128. if (response != null) {
  129. if (response.getStatusLine() != null)
  130. statusCode = response.getStatusLine().getStatusCode();
  131. HttpEntity entity = response.getEntity();
  132. // 响应内容
  133. content = EntityUtils.toString(entity, Consts.UTF_8);
  134. }
  135. } finally {
  136. response.close();
  137. }
  138. } catch (Exception e) {
  139. e.printStackTrace();
  140. } finally {
  141. httpClient.close();
  142. }
  143. }
  144. public int getStatusCode() {
  145. return statusCode;
  146. }
  147. public String getContent() throws ParseException, IOException {
  148. return content;
  149. }
  150. }