HTTPS请求大约一分钟... 我的请求网址为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();
为什么?
我的用法有问题吗?
答案 0 :(得分:-1)
执行方法会阻止您的主线程,这意味着它会在网络调用完成之前停止您的应用。您应该使用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()上删除它们