2015-11-02 31 views
0
很慢

HTTPS請求投下約一分鐘... 我的請求URL是https://auth.timeface.cn/aliyun/sts。 服務器使用TLS 1.0和AES_256_CBC編碼。 我從Chrome提示中獲得了這些消息。OkHttp要求HTTPS是在Android

所以我的代碼是這樣

String serverAddress = "https://auth.timeface.cn/aliyun/sts"; 
    OkHttpClient httpClient = new OkHttpClient(); 

    if (serverAddress.contains("https")) { 
     ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) 
       .tlsVersions(TlsVersion.TLS_1_0) 
       .cipherSuites(CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA) 
       .supportsTlsExtensions(true) 
       .build(); 

     httpClient.setConnectionSpecs(Collections.singletonList(spec)); 
     httpClient.setHostnameVerifier(new HostnameVerifier() { 
      @Override 
      public boolean verify(String hostname, SSLSession session) { 
       return true; 
      } 
     }); 
     httpClient.setConnectTimeout(1, TimeUnit.HOURS); 
    } 

    Request request = new Request.Builder() 
      .url(serverAddress) 
      .get() 
      .build(); 

    Response response = httpClient.newCall(request).execute(); 
    String responseStr = response.body().string(); 

爲什麼?

我的用法有問題嗎?

回答

0

執行方法塊主線程,這意味着它停止你的應用程序,直到網絡調用完成。您應該使用入隊方法來製作asynchronous call

String serverAddress = "https://auth.timeface.cn/aliyun/sts"; 
    OkHttpClient httpClient = new OkHttpClient(); 

    if (serverAddress.contains("https")) { 
     ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) 
       .tlsVersions(TlsVersion.TLS_1_0) 
       .cipherSuites(CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA) 
       .supportsTlsExtensions(true) 
       .build(); 

     httpClient.setConnectionSpecs(Collections.singletonList(spec)); 
     httpClient.setHostnameVerifier(new HostnameVerifier() { 
      @Override 
      public boolean verify(String hostname, SSLSession session) { 
       return true; 
      } 
     }); 
     httpClient.setConnectTimeout(1, TimeUnit.HOURS); 
    } 

Request request = new Request.Builder() 
     .url(serverAddress) 
     .build(); 

    httpClient.newCall(request).enqueue(new Callback() { 
     @Override public void onFailure(Request request, Throwable throwable) { 
     throwable.printStackTrace(); 
     } 

     @Override public void onResponse(Response response) throws IOException { 
     if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); 

     String responseStr = response.body().string(); 
     } 
    }); 

呼叫之前,只需要提供進度,並刪除他們在onFailure處()和onResponse()

+0

同步和異步調用正在慢慢... – ray