gpt4 book ai didi

Grails httpconnection 到自签名证书

转载 作者:太空宇宙 更新时间:2023-11-03 13:54:09 26 4
gpt4 key购买 nike

我有这种方法可以使用 httpconnection 发布。我收到错误 sun.security.provider.certpath.SunCertPathBuilderException: 连接到自签名网站时无法找到请求目标的有效证书路径。如何忽略证书?

def post(url, jsonData){
url = new URL(url)
def connection = url.openConnection()
connection.setRequestMethod("POST")
connection.setRequestProperty("Content-Type", "application/json");
connection.doOutput = true
def writer = new OutputStreamWriter(connection.outputStream)
writer.write(jsonData)
writer.flush()
writer.close()
connection.connect()
def resp = connection.content.text
resp
}

最佳答案

您需要创建一个函数来禁用 ssl 验证并在请求之前调用它。

这是禁用 ssl 验证所需功能的示例。

void disableSslVerification() throws NoSuchAlgorithmException, KeyManagementException{
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = [
new X509TrustManager(){
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {

}
public void checkServerTrusted(X509Certificate[] certs, String authType) {

}
}
]

// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession sslSession) {
return true;
}
};
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
}

然后你需要做
    def post(url, jsonData){
disableSslVerification()
url = new URL(url)
...

关于Grails httpconnection 到自签名证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26523522/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com