【问题标题】:OkHttp request HTTPS is very slow on AndroidOkHttp 请求 HTTPS 在 Android 上很慢
【发布时间】:2015-11-02 01:08:38
【问题描述】:

HTTPS 请求大约 1 分钟... 我的请求网址是https://auth.timeface.cn/aliyun/sts。 服务器使用 TLS 1.0 和 AES_256_CBC 编码。 我从 Chrome 提示中收到了这些消息。

所以我的代码是这样的

    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();

为什么?

我的用法有什么问题吗?

【问题讨论】:

    标签: android https okhttp


    【解决方案1】:

    execute 方法会阻塞您的主线程,这意味着它会停止您的应用程序,直到网络调用完成。您应该使用 enqueue 方法来制作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();
          }
        });
    

    只需在调用前提供 ProgressBar 并在 onFailure() 和 onResponse() 上删除它们

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-24
      • 1970-01-01
      • 1970-01-01
      • 2020-10-17
      • 1970-01-01
      • 1970-01-01
      • 2018-06-28
      相关资源
      最近更新 更多