现在的位置: 首页 > 编程技术 > 安卓开发 > 正文

Volley是否支持https

2015年06月03日 安卓开发 ⁄ 共 4794字 ⁄ 字号 评论 3 条 ⁄ 阅读 204 次
文章目录

首先是支持的,支持的是一些well kown的证书(这个可以在安卓手机中“设置”-“安全和隐私”-“受信任的凭据”中查看),如果你的证书不被支持那么你有两种方式解决:

一、信任所有证书(http://blog.denevell.org/android-trust-all-ssl-certificates.html);

二、信任指定证书(http://ogrelab.ikratko.com/using-android-volley-with-self-signed-certificate/)。

网上如果你搜索volley https有很多文章会告诉你volley默认不支持https,其实不是的,只是他的证书不被支持而已。本人半信半疑的查了一些资料和做了验证才相信。

  1. 在android开发者官网有这样一段话:

    An HTTPS Example


    Assuming you have a web server with a certificate issued by a well known CA, you can make a secure request with code as simple this:

    URL url = new URL("https://wikipedia.org");
    URLConnection urlConnection = url.openConnection();
    InputStream in = urlConnection.getInputStream();
    copyInputStreamToOutputStream(in, System.out);

    Yes, it really can be that simple. If you want to tailor the HTTP request, you can cast to an HttpURLConnection. The Android documentation for HttpURLConnection has further examples about how to deal with request and response headers, posting content, managing cookies, using proxies, caching responses, and so on. But in terms of the details for verifying certificates and hostnames, the Android framework takes care of it for you through these APIs. This is where you want to be if at all possible. That said, below are some other considerations。这个是在说,对于内置在安卓手机的ca来说,用HttpURLConnection,证书验证等等都是framework帮你去处理好的。(http://developer.android.com/training/articles/security-ssl.html

  2. 有了上面的这段话,对于volley来说。在api版本大于9的时候默认用的是HttpURLConnection,所以对于volley来说也是默认支持https。(小于9的时候本人没考虑)
  3. 对于HttpURLConnection,其直接的子类是HttpsURLConnection。他会自动处理https的协议,官网的文档解释如下:

    Secure Communication with HTTPS

    Calling openConnection() on a URL with the "https" scheme will return an HttpsURLConnection, which allows for overriding the default HostnameVerifier and SSLSocketFactory. An application-supplied SSLSocketFactory created from an SSLContext can provide a custom X509TrustManager for verifying certificate chains and a custom X509KeyManager for supplying client certificates. See HttpsURLConnection for more details.  如果需要自定义的证书之类的话可以调用这个类,volley中的源码是直接调用HttpURLConnection,因为对于well know的ca不需要自定义。(http://developer.android.com/reference/java/net/HttpURLConnection.html

  4. 最后本人拿线上服务器和测试服务器试了下,测试服务器是不支持https的,线上的支持,本人没有修改过volley代码。当本人在测试服务器地址改成https之后,用volley直接爆出异常:
    06-02 16:07:44.586: E/VolleyUtils(11123): javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x663d8c88: Failure in SSL library, usually a protocol error
    06-02 16:07:44.586: E/VolleyUtils(11123): error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:766 0x64b3c8f2:0x00000000)
    06-02 16:07:44.586: E/VolleyUtils(11123): com.android.volley.NoConnectionError: javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x663d8c88: Failure in SSL library, usually a protocol error
    06-02 16:07:44.586: E/VolleyUtils(11123): error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:766 0x64b3c8f2:0x00000000)
    06-02 16:07:44.586: E/VolleyUtils(11123): 	at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:157)
    06-02 16:07:44.586: E/VolleyUtils(11123): 	at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
    06-02 16:07:44.586: E/VolleyUtils(11123): Caused by: javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x663d8c88: Failure in SSL library, usually a protocol error
    06-02 16:07:44.586: E/VolleyUtils(11123): error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:766 0x64b3c8f2:0x00000000)
    06-02 16:07:44.586: E/VolleyUtils(11123): 	at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:436)
    06-02 16:07:44.586: E/VolleyUtils(11123): 	at libcore.net.http.HttpConnection.setupSecureSocket(HttpConnection.java:210)
    06-02 16:07:44.586: E/VolleyUtils(11123): 	at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:478)
    06-02 16:07:44.586: E/VolleyUtils(11123): 	at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:442)
    06-02 16:07:44.586: E/VolleyUtils(11123): 	at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
    06-02 16:07:44.586: E/VolleyUtils(11123): 	at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
    06-02 16:07:44.586: E/VolleyUtils(11123): 	at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
    06-02 16:07:44.586: E/VolleyUtils(11123): 	at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:486)
    06-02 16:07:44.586: E/VolleyUtils(11123): 	at libcore.net.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:134)
    06-02 16:07:44.586: E/VolleyUtils(11123): 	at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:109)
    06-02 16:07:44.586: E/VolleyUtils(11123): 	at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
    06-02 16:07:44.586: E/VolleyUtils(11123): 	... 1 more

由此可见volley是默认支持https的,我们大可放心的用,只要将http改为https即可。

目前有 3 条留言    访客:3 条, 博主:0 条

  1. 匿名 2016年07月28日 下午2:54  Δ-9楼

    留言是种美德,写点什么… 😀 😮 😯 😕

  2. 冷月 2015年07月15日 上午9:09  Δ-8楼

    上面给出的信任自定义证书的链接打不开,想请教一下博主关于volley信任自定义证书的问题,能不能给出个代码参考


    • 管理员
      admin 2015年07月15日 下午9:33  ∇地下1层

      这个我没做过,你还使用jwss.com搜索一下吧