我正在开发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)
我搜索了错误,但我找不到任何适合我的情况。任何人都可以帮助我吗?
答案 0 :(得分:4)
假设您的服务器应用程序托管在服务器计算机内,该服务器计算机具有i
为input: A, m
S := initialize a prefix array of the same size as array A
S[0] := A[0] % m
max_mod_sum := S[0]
T := initialize an empty self-balancing binary search tree
for i in [1, N):
S[i] = (S[i-1] + A[i]) % m
max_ending_at_i := S[i]
# finds the "next", aka smallest, number bigger than the argument
prev_sum := find_next(T, S[i])
if prev_sum is found:
max_ending_at_i := (S[i] - prev_sum) % m
max_mod_sum = max(max_mod_sum, max_ending_at_i)
insert(T, S[i])
return max_mod_sum
的服务器证书。然后,在验证方法内,您可以验证"Issued to"
。
"localhost"
您可以通过以下链接了解更多信息:
如果URL的主机名与对等方的标识主机名不匹配,则在握手期间使用它。
Common Problems with Hostname Verification
可能发生这种情况的一个原因是服务器配置错误。该 服务器配置了没有主题的证书 或者使用与您所在服务器匹配的替代名称字段 试图达到......
然后,在排球应用中,您可以创建"localhost"
,覆盖HostnameVerifier hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
HostnameVerifier hv =
HttpsURLConnection.getDefaultHostnameVerifier();
return hv.verify("localhost", session);
}
};
HurlStack
createConnection
。
希望这有帮助!
答案 1 :(得分:-2)
这是一种可能的解决方案
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
答案 2 :(得分:-3)
当您的互联网在http请求期间断开连接时,会发生此错误。 因此,请确保您的互联网一致,然后检查。