简体   繁体   English

对Volley Android网络库的HTTPS支持

[英]HTTPS support for Volley Android networking library

I have a project making REST calls to an HTTPS backend It which works fine on some devices, and breaks on others. 我有一个项目对HTTPS后端进行REST调用,该项目在某些设备上运行良好,而在其他设备上运行中断。

This is the error I get: 这是我得到的错误:

com.android.volley.NoConnectionError: javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x78004ee8: Failure in SSL library, usually a protocol error error:140770FCSL routinesSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:766 0x731f5d5c:0x00000000) 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)

Looking at the Volley documentation, they mention 在看排球文档时,他们提到

"You can include your own HTTPStack ( to handle SSL connections [...])" “您可以包括自己的HTTPStack(以处理SSL连接[...])”

Has someone done this for volley ? 有人为凌空抽射吗? If so can you please share your changes ? 如果可以,请您分享您的更改?

Notes: The Certificate is signed by a valid entity which was already in the trusted certificates of the devices. 注意:证书由设备的受信任证书中已经存在的有效实体签名。

Here's my solution: 这是我的解决方案:

In class Volley in method 在方法上Volley

public static RequestQueue newRequestQueue(Context context, HttpStack stack)

locate the following text: 找到以下文本:

stack = new HurlStack();

Then change this line to: 然后将此行更改为:

stack = new HurlStack(null, createSslSocketFactory());

where method createSslSocketFactory() is defined as following: 方法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;
}

I know that this is not secure, but I use it for testing purposes only. 我知道这是不安全的,但是我仅将其用于测试目的。 You can improve the security by accepting only certificates from your servers. 您可以通过仅接受来自服务器的证书来提高安全性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM