[英]Android HTTPS Request with Volley
i'm trying to do https request using volley but i get this error response 我正在尝试使用排球进行https请求,但我收到此错误响应
javax.net.ssl.SSLHandshakeException: >java.security.cert.CertPathValidatorException: Trust anchor for certification >path not found. javax.net.ssl.SSLHandshakeException:> java.security.cert.CertPathValidatorException:找不到证书的信任锚>路径。
what does this message means ? 此消息是什么意思?
The message appears because you need to add a certificate to your volley https request. 出现该消息是因为您需要向排球https请求添加证书。
To do this add HurlStack
to your request in volley. 为此,将HurlStack
添加到排球请求中。
HurlStack hurlStack = new HurlStack() {
@Override
protected HttpURLConnection createConnection(java.net.URL url)
throws IOException {
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) super
.createConnection(url);
try {
httpsURLConnection
.setSSLSocketFactory(getSSLSocketFactory(context));
// httpsURLConnection.setHostnameVerifier(getHostnameVerifier());
} catch (Exception e) {
e.printStackTrace();
}
return httpsURLConnection;
}
};
For the getSSLSocketFactory
use this function. 对于getSSLSocketFactory
使用此函数。
private SSLSocketFactory getSSLSocketFactory(Context context)
throws CertificateException, KeyStoreException, IOException,
NoSuchAlgorithmException, KeyManagementException {
// the certificate file will be stored in \app\src\main\res\raw folder path
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream caInput = context.getResources().openRawResource(
R.raw.YourCertificate);
Certificate ca = cf.generateCertificate(caInput);
caInput.close();
KeyStore keyStore = KeyStore.getInstance("BKS");
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);
TrustManager[] wrappedTrustManagers = getWrappedTrustManagers(tmf
.getTrustManagers());
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, wrappedTrustManagers, null);
return sslContext.getSocketFactory();
}
For the TrustManager
use this method: 对于TrustManager
使用以下方法:
private TrustManager[] getWrappedTrustManagers(TrustManager[] trustManagers) {
final X509TrustManager originalTrustManager = (X509TrustManager) trustManagers[0];
return new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return originalTrustManager.getAcceptedIssuers();
}
public void checkClientTrusted(X509Certificate[] certs,
String authType) {
try {
if (certs != null && certs.length > 0) {
certs[0].checkValidity();
} else {
originalTrustManager
.checkClientTrusted(certs, authType);
}
} catch (CertificateException e) {
Log.w("checkClientTrusted", e.toString());
}
}
public void checkServerTrusted(X509Certificate[] certs,
String authType) {
try {
if (certs != null && certs.length > 0) {
certs[0].checkValidity();
} else {
originalTrustManager
.checkServerTrusted(certs, authType);
}
} catch (CertificateException e) {
Log.w("checkServerTrusted", e.toString());
}
}
} };
}
Finally in the RequestQueue
call the hurlstack: 最后在RequestQueue
调用hurlstack:
RequestQueue requestQueue = Volley.newRequestQueue(context, hurlStack);
requestQueue.add(Your Request);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.