123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- package com.itheima.file;
- import com.alibaba.fastjson.JSONObject;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.auth.AuthScope;
- import org.apache.http.auth.UsernamePasswordCredentials;
- import org.apache.http.client.CredentialsProvider;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.conn.ssl.NoopHostnameVerifier;
- import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.BasicCredentialsProvider;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
- import org.apache.http.util.EntityUtils;
-
- import javax.net.ssl.SSLContext;
- import javax.net.ssl.TrustManager;
- import javax.net.ssl.X509TrustManager;
- import java.nio.charset.StandardCharsets;
- import java.security.cert.CertificateException;
- import java.security.cert.X509Certificate;
-
- public class HttpClientExample {
-
- public static void main(String[] args) throws Exception {
-
- // 忽略SSL证书验证的TrustManager(仅用于测试,不要在生产环境中使用)
- TrustManager[] trustAllCerts = new TrustManager[]{
- new X509TrustManager() {
- @Override
- public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
- }
-
- @Override
- public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
- }
-
- @Override
- public X509Certificate[] getAcceptedIssuers() {
- return new X509Certificate[]{};
- }
- }
- };
-
- // 初始化一个信任所有证书的SSLContext
- SSLContext sslContext = SSLContext.getInstance("SSL");
- sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
-
- // 使用SSLConnectionSocketFactoryBuilder来设置SSL上下文和主机名验证器
- SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
-
- // 创建连接管理器
- PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
-
- // 设置最大连接数等参数(可选)
- cm.setMaxTotal(200);
- cm.setDefaultMaxPerRoute(20);
- // 设置Basic Authentication的凭证(用户名和密码)
- String username = "po_soap1";
- String password = "z123456789";
- // 创建Basic Auth的凭据
- CredentialsProvider credsProvider = new BasicCredentialsProvider();
- credsProvider.setCredentials(
- AuthScope.ANY,
- new UsernamePasswordCredentials(username, password)
- );
-
- // 使用连接管理器和SSL上下文创建HttpClient
- CloseableHttpClient httpclient = HttpClients.custom()
- .setDefaultCredentialsProvider(credsProvider)
- .setSSLSocketFactory(sslsf) // 使用自定义的SSLSocketFactory
- .setConnectionManager(cm) // 使用自定义的连接管理器
- .build();
- // 创建请求体
- JSONObject reqBodyObj = new JSONObject();
- reqBodyObj.put("app_id", "hcmcloud");
- reqBodyObj.put("table_name", ""); // 确保这里有一个有效的值
- reqBodyObj.put("stringDate", "20210301");
- reqBodyObj.put("condition", "1=1");
- try {
- HttpPost httpPost = new HttpPost("https://podev.minmetals.com.cn:50001/RESTAdapter/BS_OA/BS_MDM/DeleteAssHrtree");
- // ... (设置请求体、头部等)
- final StringEntity reqEntity = new StringEntity(reqBodyObj.toString(), StandardCharsets.UTF_8);
- // 将HttpEntity设置为请求体
- httpPost.setEntity(reqEntity);
- HttpResponse response = httpclient.execute(httpPost);
-
- System.out.println("----------------------------------------");
- System.out.println(response.getStatusLine());
- HttpEntity entity = response.getEntity();
- if (entity != null) {
- System.out.println(EntityUtils.toString(entity));
- }
- } finally {
- httpclient.close();
- }
- }
- }
|