104 lines
3.5 KiB
Java
104 lines
3.5 KiB
Java
package com.example.sso.util;
|
|
|
|
import org.apache.http.HttpEntity;
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
import org.apache.http.client.methods.HttpPost;
|
|
import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
|
import org.apache.http.entity.ContentType;
|
|
import org.apache.http.entity.StringEntity;
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
import org.apache.http.impl.client.HttpClients;
|
|
import org.apache.http.ssl.SSLContexts;
|
|
import org.apache.http.util.EntityUtils;
|
|
|
|
import javax.net.ssl.SSLContext;
|
|
import javax.net.ssl.TrustManager;
|
|
import javax.net.ssl.X509TrustManager;
|
|
import java.security.cert.CertificateException;
|
|
import java.security.cert.X509Certificate;
|
|
|
|
public class FuWuuUil {
|
|
|
|
public static String select(String jsonBody) throws Exception {
|
|
SSLContext sslContext = createTrustAllSSLContext();
|
|
CloseableHttpClient httpClient = createHttpClient(sslContext);
|
|
|
|
// 创建 POST 请求对象
|
|
HttpPost httpPost = new HttpPost("https://www.jiyuankeshang.com/api/v5/app/entry/data/list");
|
|
|
|
String responseBody = null;
|
|
try {
|
|
// 设置请求头
|
|
httpPost.setHeader("Content-Type", "application/json");
|
|
httpPost.setHeader("Authorization", "Bearer " + "BkIyzlh1onqnqu9cQ3ralDQBjECn97ex");
|
|
|
|
|
|
StringEntity entity = new StringEntity(jsonBody, ContentType.APPLICATION_JSON);
|
|
httpPost.setEntity(entity);
|
|
|
|
// 执行请求,获取响应对象
|
|
CloseableHttpResponse response = httpClient.execute(httpPost);
|
|
|
|
try {
|
|
// 从响应对象中获取响应实体
|
|
HttpEntity responseEntity = response.getEntity();
|
|
|
|
// 处理响应数据
|
|
responseBody = EntityUtils.toString(responseEntity);
|
|
|
|
} finally {
|
|
// 关闭响应对象
|
|
response.close();
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
} finally {
|
|
try {
|
|
// 关闭 HttpClient
|
|
httpClient.close();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
return responseBody;
|
|
}
|
|
|
|
|
|
private static SSLContext createTrustAllSSLContext() throws Exception {
|
|
// 创建一个信任所有证书的 TrustManager
|
|
TrustManager[] trustAllCerts = new TrustManager[]{
|
|
new X509TrustManager() {
|
|
@Override
|
|
public X509Certificate[] getAcceptedIssuers() {
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
|
|
}
|
|
|
|
@Override
|
|
public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
|
|
}
|
|
}
|
|
};
|
|
|
|
// 创建 SSL 上下文并初始化
|
|
SSLContext sslContext = SSLContexts.custom().build();
|
|
sslContext.init(null, trustAllCerts, null);
|
|
|
|
return sslContext;
|
|
}
|
|
|
|
private static CloseableHttpClient createHttpClient(SSLContext sslContext) {
|
|
// 创建 HttpClient 并设置信任所有证书的 SSL 上下文
|
|
return HttpClients.custom()
|
|
.setSSLContext(sslContext)
|
|
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
|
|
.build();
|
|
}
|
|
|
|
|
|
|
|
}
|