一、简价
今天在调用一个支付平台时,返回结果为ssl证书未被信任。经过查询。
二、代码
、、、
package com.best.payment;
import com.best.payment.RSA.AlipaySign;
import com.best.payment.api.SslUtil;
import javax.net.ssl.HttpsURLConnection;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
/**
@author redkapok
@Description 类的描述
@date 2024/7/26 17:36
-
@Company
*/
public class HttpsPostRequest {public static void main(String[] args) throws Exception {
//获以当前时间
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String currentDate = simpleDateFormat.format(new Date());
//私钥
String signPrivateKey = "MIIEvgIBADANBgk";
// 目标URL
String targetURL = "https://open-sea.worldfirst.com/amsin/api/v1/business/account/inquiryStatementList";
// 创建URL对象
URL url = new URL(targetURL);
//忽略证书认证
SslUtil.ignoreSsl();
// 打开连接
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
// 设置请求方法为POST
connection.setRequestMethod("POST");
// 设置允许输入输出
connection.setDoOutput(true);
// 设置请求属性(根据需要设置,例如Content-Type)
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("request-time", currentDate);
connection.setRequestProperty("client-id", "94");
// 写入POST数据
String postData = "{\"pageNumber\":1,\"currencyList\":[],\"pageSize\":10,\"startTime\":\"2024-07-01T00:00:00Z\",\"endTime\":\"2024-07-26T00:00:00Z\",\"transactionTypeList\":[]}";
connection.setRequestProperty("Signature", AlipaySign.sign("POST","/amsin/api/v1/business/account/inquiryStatementList",04494",currentDate, postData,signPrivateKey));
byte[] outputInBytes = postData.getBytes("UTF-8");
try(
OutputStream os = connection.getOutputStream()) {
os.write(outputInBytes);
InputStream is=connection.getInputStream();
InputStreamReader reader=new InputStreamReader(is);
char[] buffer=new char[1024];
StringBuilder out = new StringBuilder();
for(int read;(read=reader.read(buffer,0,buffer.length))>0;){
out.append(buffer,0,read);
}
System.out.println(out.toString());
}
// 获取响应码
int responseCode = connection.getResponseCode();
System.out.println("Response Code: " + responseCode);
// 读取响应(根据需要处理)
// ...
// 关闭连接
connection.disconnect();
}
}
、、、
、、、
package com.best.payment.api;
import javax.net.ssl.*;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
/**
@author redkapok
@Description 类的描述
@date 2024/7/26 18:13
-
@Company 广州市灏仟亿贸易有限公司
*/
public class SslUtil {private static void trustAllHttpsCertificates()throws Exception{
TrustManager[] trustAllCerts=new TrustManager[1];
TrustManager tm=new miTM();
trustAllCerts[0]=tm;
SSLContext sc=SSLContext.getInstance("SSL");
sc.init(null,trustAllCerts,null);
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}static class miTM implements TrustManager,X509TrustManager{
@Override public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { return ; } @Override public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { return ; } @Override public X509Certificate[] getAcceptedIssuers() { return null; }}
public static void ignoreSsl()throws Exception{
HostnameVerifier hv=new HostnameVerifier() {
@Override
public boolean verify(String s, SSLSession sslSession) {
return true;
}
};
trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier(hv);
}
}
、、、