gpt4 book ai didi

java - 无法使用 HTTPS 连接到套接字

转载 作者:行者123 更新时间:2023-12-01 19:31:52 24 4
gpt4 key购买 nike

TrustManager[] trustManager = new TrustManager[]{
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}

public void checkClientTrusted(X509Certificate[] certificate, String str) {
}

public void checkServerTrusted(X509Certificate[] certificate, String str) {
}
}
};

SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("TLS");
try {
sslContext.init(null, trustManager, null);
} catch (KeyManagementException e) {
e.printStackTrace();
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}

HostnameVerifier myHostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
};

SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient okHttpClient = new OkHttpClient.Builder().hostnameVerifier(myHostnameVerifier)
.sslSocketFactory(sslSocketFactory,X509TrustManager trustManager).build();

我在sslSocketFactory(sslSocketFactory,X509TrustManager trustManager).build();附近遇到错误

最佳答案

 TrustManager[] trustManager = new X509TrustManager[]{
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}

public void checkClientTrusted(X509Certificate[] certificate,
String str) {
}

public void checkServerTrusted(X509Certificate[] certificate,
String str) {
}
}
};

SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("TLS");
try {
sslContext.init(null, trustManager, null);
} catch (KeyManagementException e) {
e.printStackTrace();
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}

HostnameVerifier myHostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
};

SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient okHttpClient = new OkHttpClient.Builder().hostnameVerifier(myHostnameVerifier).sslSocketFactory(sslSocketFactory).build();

关于java - 无法使用 HTTPS 连接到套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59496456/

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