新增:利元亨现场3方接口交互代码提交

This commit is contained in:
yangyang01000846
2025-12-22 13:41:00 +08:00
parent c7052e0b7e
commit b6a1fff060
13 changed files with 436 additions and 171 deletions

View File

@@ -1,9 +1,13 @@
package com.sdm.common.utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
@@ -14,6 +18,7 @@ import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustAllStrategy;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
@@ -28,11 +33,13 @@ import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.List;
import java.util.Map;
@Slf4j
@@ -44,7 +51,7 @@ public class HttpUtil {
public static CloseableHttpClient createSSLClientDefault() throws Exception {
//信任所有
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(), NoopHostnameVerifier.INSTANCE
SSLContexts.custom().loadTrustMaterial(null, new TrustAllStrategy()).build(), NoopHostnameVerifier.INSTANCE
);
//创建自定义连接套接字工厂的注册表以支持
//协议方案。
@@ -438,8 +445,7 @@ public class HttpUtil {
try {
HttpPost httpPost = new HttpPost(url);
//设置header
// httpPost.setHeader("Content-Type", "application/json");
if (headers.size() > 0) {
if (MapUtils.isNotEmpty(headers)) {
for (String key : headers.keySet()) {
httpPost.addHeader(key, headers.get(key));
}
@@ -472,4 +478,50 @@ public class HttpUtil {
}
return result;
}
public static String httpPostForm(String url, Map<String, String> headers, List<NameValuePair> params) throws Exception {
if (httpclient == null) {
httpclient = createSSLClientDefault();
}
CloseableHttpClient hp = httpclient;
CloseableHttpResponse response = null;
String result = "";
try {
HttpPost httpPost = new HttpPost(url);
//设置header
if (MapUtils.isNotEmpty(headers)) {
for (String key : headers.keySet()) {
httpPost.addHeader(key, headers.get(key));
}
}
//填充参数
UrlEncodedFormEntity paramEntity = new UrlEncodedFormEntity(params, Consts.UTF_8);
httpPost.setEntity(paramEntity);
//发起请求
response = hp.execute(httpPost);
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, "UTF-8");
//处理返回结果
if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() > 300) {
throw new Exception(result);
}
EntityUtils.consume(entity);
} catch (IOException e) {
log.error("httpclient IO error:" + e);
result = e.toString();
} finally {
if (response != null) {
try {
EntityUtils.consume(response.getEntity());
response.close();
} catch (IOException e) {
log.error("释放response错误");
e.printStackTrace();
}
}
hp.close();
}
return result;
}
}