gpt4 book ai didi

android - 自签名证书异常 Hostname Verification failed

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

我正在尝试使用 httpsurlconnection 发送数据。我将 .pem 证书导入到 android keystore 中并创建了 ssl 上下文。现在一切正常,除了当我从 Android 通过服务器发送一些数据时它说主机名未验证。

 HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
AssetManager assetManager = MainActivity.ctx.getAssets();
InputStream requestXML = assetManager.open("requestdump");
String requestString = convertStreamToString(requestXML);
java.net.URL url = new URL("https://nn.nnn.nnn.nnn:xxxx");
HttpsURLConnection urlConnection = (HttpsURLConnection)url.openConnection();
urlConnection.setSSLSocketFactory(getSSLSocketFactory());
urlConnection.setReadTimeout(10000);
urlConnection.setConnectTimeout(15000);
urlConnection.setRequestMethod("POST");
urlConnection.setHostnameVerifier(hostnameVerifier );
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
byte[] outputInBytes = requestString.getBytes("UTF-8");
OutputStream os = urlConnection.getOutputStream();
os.write( outputInBytes );
os.close();
InputStream in = urlConnection.getInputStream();

此代码正在运行并在 ssl 启用服务器上发送数据。我试过这段代码:

HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
urlConnection.setHostnameVerifier(hostnameVerifier );

但我不想使用这种方法。

如果没有这种方法,我将无法验证主机名。任何人都可以建议如何在不使用 ALLOW_ALL_HOSTNAME_VERIFIE 的情况下使用自签名证书通过服务器发送数据。

如有任何帮助,我们将不胜感激。

最佳答案

修复证书,使其主机名正确。这意味着使用新的 key 对重新开始。

关于android - 自签名证书异常 Hostname Verification failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29286017/

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