Oli*_*erM 2 rest https android http android-volley
我有一个项目对HTTPS后端进行REST调用它在某些设备上工作正常,并在其他设备上中断.
这是我得到的错误:
com.android.volley.NoConnectionError:javax.net.ssl.SSLHandshakeException:javax.net.ssl.SSLProtocolException:SSL握手中止:ssl = 0x78004ee8:SSL库失败,通常是协议错误错误:140770FCSL例程SL23_GET_SERVER_HELLO:未知协议(外部/openssl/ssl/s23_clnt.c:766 0x731f5d5c:0x00000000)
看一下Volley文档,他们提到了
"你可以包含自己的HTTPStack(以处理SSL连接[...])"
有人这样做是为了凌空吗?如果是这样,请分享您的更改?
注意:证书由已在设备的可信证书中的有效实体签名.
小智 6
这是我的解决方案:
在课堂Volley上的方法
public static RequestQueue newRequestQueue(Context context, HttpStack stack)
Run Code Online (Sandbox Code Playgroud)
找到以下文字:
stack = new HurlStack();
Run Code Online (Sandbox Code Playgroud)
然后将此行更改为:
stack = new HurlStack(null, createSslSocketFactory());
Run Code Online (Sandbox Code Playgroud)
其中method createSslSocketFactory()的定义如下:
private static SSLSocketFactory createSslSocketFactory() {
TrustManager[] byPassTrustManagers = new TrustManager[]{new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}
}};
SSLContext sslContext = null;
SSLSocketFactory sslSocketFactory = null;
try {
sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, byPassTrustManagers, new SecureRandom());
sslSocketFactory = sslContext.getSocketFactory();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
Log.e(TAG, StringUtils.EMPTY, e);
} catch (KeyManagementException e) {
Log.e(TAG, StringUtils.EMPTY, e);
}
return sslSocketFactory;
}
Run Code Online (Sandbox Code Playgroud)
我知道这不安全,但我仅将其用于测试目的.您可以通过仅接受来自服务器的证书来提高安全性.
| 归档时间: |
|
| 查看次数: |
6968 次 |
| 最近记录: |