2016-04-14 58 views
1

詳細使用HttpsURLConnection的:出現SSLHandshakeException Android中關於我的應用程序

compileSdkVersion 22 
    buildToolsVersion '22.0.1' 



    defaultConfig { 
     applicationId "My application id" 
     minSdkVersion 13 
     targetSdkVersion 22 
     versionCode 4 
     versionName "2" 
    } 

我使用HttpsURLConnection的用於登錄authentication.But現在我面臨javax.net.ssl.SSLHandshakeException:javax.net.ssl中。 SSLProtocolException也運行在Android lollipop.Authentication設備上運行的操作系統版本的設備上運行正常。請讓我知道你對這個問題的建議。

這裏是我使用的代碼:

HttpsURLConnection conn = null; 
    try { 
     URL url = new URL("URL to my application"); 

     String authStr = username + ":" + password; 

     String authEncoded = Base64.encodeToString(authStr.getBytes(), Base64.NO_WRAP); 
     conn = (HttpsURLConnection) url.openConnection(); 
     conn.setRequestMethod("POST"); 
     conn.setRequestProperty("Authorization", "Basic " + authEncoded); 
     conn.setDoOutput(true); 

     conn.setConnectTimeout(120000); 
     int code = conn.getResponseCode(); 

     if(code ==200){ 
      for(int i=0;i<conn.getHeaderFields().size();i++){ 
       String value = conn.getHeaderField(i); 

       HeaderElement headerElement = BasicHeaderValueParser.parseHeaderElement(value, new BasicHeaderValueParser()); 

       if(headerElement.getName().equals("SMSESSION")){ 

        cookieString = SMSESSION+"="+headerElement.getValue(); 
        Log.d("validhdvaluelogin",cookieString); 
        cookieeditor.putString(ArticleListActivity.CURRENT_COOKIE, 
          cookieString); 
        cookieeditor.apply(); 

        return true; 
       } 


      } 
     }else { 
      return false; 
     } 



    } 

    catch (Exception e) { 
     e.printStackTrace(); 
     return false; 
    } 


    if (conn != null) { 
     conn.disconnect(); 
    } 

    return false; 

這裏是例外:

javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL  
handshake aborted: ssl=0xb87b0350: Failure in SSL library, usually a  
protocol error 
error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake    
failure (external/openssl/ssl/s23_clnt.c:741 0xa9092990:0x00000000) 
at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:448) 
at com.android.org.conscrypt.OpenSSLSocketImpl$SSLInputStream.<init>(OpenSSLSocketImpl.java:661) 
at com.android.org.conscrypt.OpenSSLSocketImpl.getInputStream(OpenSSLSocketImpl.java:632) 
at org.apache.http.impl.io.SocketInputBuffer.<init>(SocketInputBuffer.java:70) 
at org.apache.http.impl.SocketHttpClientConnection.createSessionInputBuffer(SocketHttpClientConnection.java:83) 
at org.apache.http.impl.conn.DefaultClientConnection.createSessionInputBuffer(DefaultClientConnection.java:170) 
at org.apache.http.impl.SocketHttpClientConnection.bind(SocketHttpClientConnection.java:106) 
at org.apache.http.impl.conn.DefaultClientConnection.openCompleted(DefaultClientConnection.java:129) 
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:172) 
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 
at com.ericsson.labs.it.infoservicemx.network.AuthComm.login(AuthComm.java:148) 
at com.ericsson.labs.it.infoservicemx.LoginActivity$UserLoginTask.doInBackground(LoginActivity.java:312) 
at com.ericsson.labs.it.infoservicemx.LoginActivity$UserLoginTask.doInBackground(LoginActivity.java:230) 
at android.os.AsyncTask$2.call(AsyncTask.java:288) 
at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
at java.lang.Thread.run(Thread.java:841) 
Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb87b0350: Failure in SSL library, usually a protocol error 
error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure (external/openssl/ssl/s23_clnt.c:741 0xa9092990:0x00000000) 
at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method) 
at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:405) 
... 23 more 
+0

我認爲它的相關問題http://stackoverflow.com/a/27928360/3436179 – Alexander

+0

該帖子中提供的解決方案對我無效 –

回答

2

我終於找到了解決我的問題。 通過使用NetCipher庫進行httpsurlconnection。 而不是使用:

HttpsUrlConnection conn = (HttpsURLConnection) url.openConnection(); 

用途:

HttpsUrlConnection conn = NetCipher.getHttpsURLConnection(url); 

這解決了我的SSLv3握手的問題。

+0

這幫助我解決了這個問題。 –

相關問題