7049ec5dc111da451eca36ebc5f1ba36680c4dcf.svn-base 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package xin.glue.user.common;
  2. import java.io.IOException;
  3. import java.security.KeyManagementException;
  4. import java.security.KeyStoreException;
  5. import java.security.NoSuchAlgorithmException;
  6. import java.security.cert.CertificateException;
  7. import java.security.cert.X509Certificate;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.List;
  11. import java.util.Map;
  12. import javax.net.ssl.HostnameVerifier;
  13. import javax.net.ssl.SSLContext;
  14. import org.apache.http.HttpEntity;
  15. import org.apache.http.NameValuePair;
  16. import org.apache.http.client.entity.UrlEncodedFormEntity;
  17. import org.apache.http.client.methods.CloseableHttpResponse;
  18. import org.apache.http.client.methods.HttpPost;
  19. import org.apache.http.conn.ssl.NoopHostnameVerifier;
  20. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  21. import org.apache.http.conn.ssl.TrustStrategy;
  22. import org.apache.http.impl.client.CloseableHttpClient;
  23. import org.apache.http.impl.client.HttpClients;
  24. import org.apache.http.message.BasicNameValuePair;
  25. import org.apache.http.ssl.SSLContextBuilder;
  26. import org.apache.http.util.EntityUtils;
  27. //import org.slf4j.Logger;
  28. //import org.slf4j.LoggerFactory;
  29. import com.alibaba.fastjson.JSON;
  30. import com.alibaba.fastjson.JSONObject;
  31. public class UIJ030055 {
  32. //private static Logger logger = LoggerFactory.getLogger(UIJ030055.class);
  33. static CloseableHttpClient httpClient;
  34. static CloseableHttpResponse httpResponse;
  35. public static CloseableHttpClient createSSLClientDefault() {
  36. try {
  37. SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
  38. // 信任所有
  39. public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
  40. return true;
  41. }
  42. }).build();
  43. HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
  44. SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
  45. return HttpClients.custom().setSSLSocketFactory(sslsf).build();
  46. } catch (KeyManagementException e) {
  47. e.printStackTrace();
  48. } catch (NoSuchAlgorithmException e) {
  49. e.printStackTrace();
  50. } catch (KeyStoreException e) {
  51. e.printStackTrace();
  52. }
  53. return HttpClients.createDefault();
  54. }
  55. /**
  56. * 发送https请求
  57. *
  58. * @param jsonPara
  59. * @throws Exception
  60. */
  61. public static String sendByHttp(Map<String, Object> params, String url) {
  62. try {
  63. HttpPost httpPost = new HttpPost(url);
  64. List<NameValuePair> listNVP = new ArrayList<NameValuePair>();
  65. if (params != null) {
  66. for (String key : params.keySet()) {
  67. listNVP.add(new BasicNameValuePair(key, params.get(key).toString()));
  68. }
  69. }
  70. UrlEncodedFormEntity entity = new UrlEncodedFormEntity(listNVP, "UTF-8");
  71. //logger.info("创建请求httpPost-URL={},params={}", url, listNVP);
  72. httpPost.setEntity(entity);
  73. httpClient = UIJ030055.createSSLClientDefault();
  74. httpResponse = httpClient.execute(httpPost);
  75. HttpEntity httpEntity = httpResponse.getEntity();
  76. if (httpEntity != null) {
  77. String jsObject = EntityUtils.toString(httpEntity, "UTF-8");
  78. JSONObject jo = JSONObject.parseObject(new String(jsObject));
  79. String jsonString = "["+JSON.toJSONString(jo)+"]";
  80. return jsonString;
  81. } else {
  82. return null;
  83. }
  84. } catch (Exception e) {
  85. e.printStackTrace();
  86. return null;
  87. } finally {
  88. try {
  89. httpResponse.close();
  90. httpClient.close();
  91. // logger.info("请求流关闭完成");
  92. } catch (IOException e) {
  93. // logger.info("请求流关闭出错");
  94. e.printStackTrace();
  95. }
  96. }
  97. }
  98. public static void main(String[] args) throws Exception {
  99. Map<String, Object> map = new HashMap<String, Object>();
  100. map.put("authCode", "FX:123");
  101. map.put("userName", "jianghaida");
  102. map.put("pwd", "jianghaida");
  103. System.out.println(UIJ030055.sendByHttp(map, "https://218.87.96.132/xg56/webservice/webWaybill/queryIsChecked/20200616S227"));;
  104. }
  105. }