HttpClientExample.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package com.itheima.file;
  2. import com.alibaba.fastjson.JSONObject;
  3. import org.apache.http.HttpEntity;
  4. import org.apache.http.HttpResponse;
  5. import org.apache.http.auth.AuthScope;
  6. import org.apache.http.auth.UsernamePasswordCredentials;
  7. import org.apache.http.client.CredentialsProvider;
  8. import org.apache.http.client.methods.HttpPost;
  9. import org.apache.http.conn.ssl.NoopHostnameVerifier;
  10. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  11. import org.apache.http.entity.StringEntity;
  12. import org.apache.http.impl.client.BasicCredentialsProvider;
  13. import org.apache.http.impl.client.CloseableHttpClient;
  14. import org.apache.http.impl.client.HttpClients;
  15. import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
  16. import org.apache.http.util.EntityUtils;
  17. import javax.net.ssl.SSLContext;
  18. import javax.net.ssl.TrustManager;
  19. import javax.net.ssl.X509TrustManager;
  20. import java.nio.charset.StandardCharsets;
  21. import java.security.cert.CertificateException;
  22. import java.security.cert.X509Certificate;
  23. public class HttpClientExample {
  24. public static void main(String[] args) throws Exception {
  25. // 忽略SSL证书验证的TrustManager(仅用于测试,不要在生产环境中使用)
  26. TrustManager[] trustAllCerts = new TrustManager[]{
  27. new X509TrustManager() {
  28. @Override
  29. public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
  30. }
  31. @Override
  32. public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
  33. }
  34. @Override
  35. public X509Certificate[] getAcceptedIssuers() {
  36. return new X509Certificate[]{};
  37. }
  38. }
  39. };
  40. // 初始化一个信任所有证书的SSLContext
  41. SSLContext sslContext = SSLContext.getInstance("SSL");
  42. sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
  43. // 使用SSLConnectionSocketFactoryBuilder来设置SSL上下文和主机名验证器
  44. SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
  45. // 创建连接管理器
  46. PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
  47. // 设置最大连接数等参数(可选)
  48. cm.setMaxTotal(200);
  49. cm.setDefaultMaxPerRoute(20);
  50. // 设置Basic Authentication的凭证(用户名和密码)
  51. String username = "po_soap1";
  52. String password = "z123456789";
  53. // 创建Basic Auth的凭据
  54. CredentialsProvider credsProvider = new BasicCredentialsProvider();
  55. credsProvider.setCredentials(
  56. AuthScope.ANY,
  57. new UsernamePasswordCredentials(username, password)
  58. );
  59. // 使用连接管理器和SSL上下文创建HttpClient
  60. CloseableHttpClient httpclient = HttpClients.custom()
  61. .setDefaultCredentialsProvider(credsProvider)
  62. .setSSLSocketFactory(sslsf) // 使用自定义的SSLSocketFactory
  63. .setConnectionManager(cm) // 使用自定义的连接管理器
  64. .build();
  65. // 创建请求体
  66. JSONObject reqBodyObj = new JSONObject();
  67. reqBodyObj.put("app_id", "hcmcloud");
  68. reqBodyObj.put("table_name", ""); // 确保这里有一个有效的值
  69. reqBodyObj.put("stringDate", "20210301");
  70. reqBodyObj.put("condition", "1=1");
  71. try {
  72. HttpPost httpPost = new HttpPost("https://podev.minmetals.com.cn:50001/RESTAdapter/BS_OA/BS_MDM/DeleteAssHrtree");
  73. // ... (设置请求体、头部等)
  74. final StringEntity reqEntity = new StringEntity(reqBodyObj.toString(), StandardCharsets.UTF_8);
  75. // 将HttpEntity设置为请求体
  76. httpPost.setEntity(reqEntity);
  77. HttpResponse response = httpclient.execute(httpPost);
  78. System.out.println("----------------------------------------");
  79. System.out.println(response.getStatusLine());
  80. HttpEntity entity = response.getEntity();
  81. if (entity != null) {
  82. System.out.println(EntityUtils.toString(entity));
  83. }
  84. } finally {
  85. httpclient.close();
  86. }
  87. }
  88. }