gpt4 book ai didi

java - Volley SSL - 主机名未验证

转载 作者:太空狗 更新时间:2023-10-29 15:26:14 24 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,我需要访问一个 HTTPS 地址。我正在使用 Volley 请求我的数据,但现在我收到此错误 com.android.volley.NoConnectionError: java.io.IOException: Hostname '<address>' was not verified

为了获得 SSL 工厂,我做了这个:

private static SSLSocketFactory getSocketFactory() {
SSLSocketFactory socketFactory = null;


try {
final CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
final BufferedInputStream bis = new BufferedInputStream(sContext.getResources().openRawResource(R.raw.cert)) ;

Certificate certificate;
try {
certificate = certificateFactory.generateCertificate(bis);
} finally {
bis.close();
}

final String keyStoreType = KeyStore.getDefaultType();
final KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", certificate);

final String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);
final SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(), null);
socketFactory = sslContext.getSocketFactory();
} catch (Exception e) {
Log.w(TAG, Log.getStackTraceString(e));
}
return socketFactory;
}

队列初始化:

final HttpStack stack = new HurlStack(null, getSocketFactory());
final Cache cache = new NoCache();
final Network network = new BasicNetwork(stack);
sRequestQueue = new RequestQueue(cache, network);
sRequestQueue.start();

这是堆栈跟踪:

    com.android.volley.NoConnectionError: java.io.IOException: Hostname 'url' was not verified
09-04 11:15:33.198 W/System.err﹕ at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:151)
09-04 11:15:33.198 W/System.err﹕ at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
09-04 11:15:33.198 W/System.err﹕ Caused by: java.io.IOException: Hostname 'hml.services.vivara.com.br' was not verified
09-04 11:15:33.198 W/System.err﹕ at com.android.okhttp.Connection.upgradeToTls(Connection.java:201)
09-04 11:15:33.198 W/System.err﹕ at com.android.okhttp.Connection.connect(Connection.java:151)
09-04 11:15:33.198 W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:276)
09-04 11:15:33.198 W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
09-04 11:15:33.198 W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373)
09-04 11:15:33.198 W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
09-04 11:15:33.198 W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:208)
09-04 11:15:33.198 W/System.err﹕ at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
09-04 11:15:33.198 W/System.err﹕ at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
09-04 11:15:33.198 W/System.err﹕ at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:240)
09-04 11:15:33.198 W/System.err﹕ at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:210)
09-04 11:15:33.198 W/System.err﹕ at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:106)
09-04 11:15:33.198 W/System.err﹕ at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)

我搜索了错误,但没有找到适合我的情况的任何内容。谁能帮帮我?

最佳答案

假设您的服务器应用程序托管在一台服务器机器中,该机器具有服务器证书,例如,“Issued to”“localhost”。然后,在验证方法中,您可以验证 "localhost"

HostnameVerifier hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
HostnameVerifier hv =
HttpsURLConnection.getDefaultHostnameVerifier();
return hv.verify("localhost", session);
}
};

您可以在以下链接中阅读更多内容:

  1. HostnameVerifier

    It is to be used during a handshake if the URL's hostname does not match the peer's identification hostname.

  2. Common Problems with Hostname Verification

    One reason this can happen is due to a server configuration error. The server is configured with a certificate that does not have a subject or subject alternative name fields that match the server you are trying to reach...

然后,在您的 Volley 应用中,您可以创建一个 HurlStack,覆盖 createConnection,其中 setHostnameVerifier httpsURLConnection

希望这对您有所帮助!

关于java - Volley SSL - 主机名未验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32403479/

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