HttpsClientUtil.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package com.kmall.admin.haikong.utils;
  2. import org.apache.http.HttpEntity;
  3. import org.apache.http.HttpResponse;
  4. import org.apache.http.HttpStatus;
  5. import org.apache.http.NameValuePair;
  6. import org.apache.http.client.config.RequestConfig;
  7. import org.apache.http.client.entity.UrlEncodedFormEntity;
  8. import org.apache.http.client.methods.HttpPost;
  9. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  10. import org.apache.http.entity.ContentType;
  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 org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18. import javax.net.ssl.*;
  19. import java.io.IOException;
  20. import java.security.KeyManagementException;
  21. import java.security.NoSuchAlgorithmException;
  22. import java.security.cert.X509Certificate;
  23. import java.util.ArrayList;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. import java.util.Map;
  27. public class HttpsClientUtil {
  28. private static final Logger log = LoggerFactory.getLogger(HttpsClientUtil.class);
  29. private static RequestConfig requestConfig = RequestConfig.custom()
  30. .setSocketTimeout(5000)
  31. .setConnectTimeout(5000)
  32. .setConnectionRequestTimeout(5000)
  33. .build();//设置请求和传输超时时间
  34. private final static String charset = "UTF-8";
  35. /**
  36. * 忽略SSL证书校验的POST请求.
  37. * @param url
  38. * @return
  39. * @throws IOException
  40. */
  41. public static String post (String url, StringEntity stringEntity){
  42. HttpResponse res = null;
  43. try {
  44. stringEntity.setContentType(ContentType.APPLICATION_JSON.toString());
  45. res = postnew(url,stringEntity);
  46. return EntityUtils.toString(res.getEntity(), charset);
  47. } catch (IOException e) {
  48. log.error("数据请求失败",e);
  49. }
  50. return null;
  51. }
  52. /**
  53. * 忽略SSL证书校验的POST请求.
  54. * @param url
  55. * @return
  56. * @throws IOException
  57. */
  58. public static String vmcconnectPost(String url, StringEntity stringEntity){
  59. HttpResponse res = null;
  60. try {
  61. stringEntity.setContentType(ContentType.MULTIPART_FORM_DATA.toString());
  62. res = postnew(url,stringEntity);
  63. return EntityUtils.toString(res.getEntity(), charset);
  64. } catch (IOException e) {
  65. log.error("请求免税MALL接口异常",e);
  66. }
  67. return null;
  68. }
  69. /**
  70. * 忽略SSL证书校验的POST.
  71. * @param httpUrl
  72. * @param map Map
  73. * @return
  74. */
  75. public static String post(String httpUrl, Map<String, Object> map) throws IOException {
  76. //设置参数
  77. List<NameValuePair> list = new ArrayList<NameValuePair>();
  78. Iterator iterator = map.entrySet().iterator();
  79. while(iterator.hasNext()){
  80. Map.Entry<String,String> entry = (Map.Entry<String, String>) iterator.next();
  81. list.add(new BasicNameValuePair(entry.getKey(),String.valueOf(entry.getValue())));
  82. }
  83. if(list.size() > 0){
  84. UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset);
  85. HttpResponse res = null;
  86. if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
  87. return EntityUtils.toString(res.getEntity(), charset);
  88. }
  89. }
  90. return null;
  91. }
  92. /**
  93. * 忽略SSL证书校验的POST.
  94. * @param url
  95. * @param httpEntity HttpEntity
  96. * @return
  97. * @throws IOException
  98. */
  99. public static HttpResponse post(String url, HttpEntity httpEntity) throws IOException {
  100. CloseableHttpClient sslClient = createSSLClient();
  101. HttpPost post = new HttpPost(url);
  102. post.setEntity(httpEntity);
  103. post.setConfig(requestConfig);
  104. HttpResponse response = sslClient.execute(post);
  105. return response;
  106. }
  107. /**
  108. * 忽略SSL证书校验的POST.
  109. * @param url
  110. * @param httpEntity HttpEntity
  111. * @return
  112. * @throws IOException
  113. */
  114. public static HttpResponse postnew(String url, HttpEntity httpEntity) throws IOException {
  115. CloseableHttpClient sslClient = createSSLClient();
  116. HttpPost post = new HttpPost(url);
  117. post.setEntity(httpEntity);
  118. post.setConfig(requestConfig);
  119. HttpResponse response = sslClient.execute(post);
  120. return response;
  121. }
  122. /**
  123. * 忽略SSL证书校验的CloseableHttpClient.
  124. * @return
  125. */
  126. public static CloseableHttpClient createSSLClient(){
  127. // 在JSSE中,证书信任管理器类就是实现了接口X509TrustManager的类。我们可以自己实现该接口,让它信任我们指定的证书。
  128. // 创建SSLContext对象,并使用我们指定的信任管理器初始化,信任所有
  129. X509TrustManager x509mgr = new X509TrustManager() {
  130. //  该方法检查客户端的证书,若不信任该证书则抛出异常
  131. public void checkClientTrusted(X509Certificate[] xcs, String string) {
  132. }
  133. //   该方法检查服务端的证书,若不信任该证书则抛出异常
  134. public void checkServerTrusted(X509Certificate[] xcs, String string) {
  135. }
  136. //  返回受信任的X509证书数组。
  137. public X509Certificate[] getAcceptedIssuers() {
  138. return null;
  139. }
  140. };
  141. SSLContext sslContext = null;
  142. try {
  143. sslContext = SSLContext.getInstance("TLS");
  144. sslContext.init(null, new TrustManager[] { x509mgr }, null);
  145. SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new HostnameVerifier() {
  146. @Override
  147. public boolean verify(String hostname, SSLSession sslSession) {
  148. // 对hostname不做验证信任所有,此处有安全隐患
  149. return true;
  150. }
  151. });
  152. return HttpClients.custom().setSSLSocketFactory(sslsf).build();
  153. } catch (NoSuchAlgorithmException e) {
  154. log.error(e.getMessage(), e);
  155. } catch (KeyManagementException e) {
  156. log.error(e.getMessage(), e);
  157. }
  158. return HttpClients.createDefault();
  159. }
  160. }