最近希望提高App访问API的速度,于是采用IP直连的方式,因为我用的库是okhttp,如果直接请求IP地址可能会报错:
javax/net/ssl/SSLProtocolException: SSL handshake aborted: ssl=0x7f206dd280: Failure in SSL library, usually a protocol error
error:10000410:SSL routines:OPENSSL_internal:SSLV3_ALERT_HANDSHAKE_FAILURE (external/boringssl/src/ssl/tls_record.c:504 0x7f25b26bc0:0x00000001)
跟后端程序员讨论了一下,他们如下说:
需要我在host里配置我的域名,用这个域名去和ip做校验,通过的话,才可以正常请求。
但是这样如何在okhttp里体现呢,下面直接上相应的代码:
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
Interceptor headers = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
HttpUrl newUrl = original.url().newBuilder().host(我的域名).build();
Request completeRequest = original.newBuilder()
.url(newUrl)
.build();
Request.Builder requestBuilder = completeRequest.newBuilder();
Request request = requestBuilder.build();
return chain.proceed(request);
}
};
httpClient.interceptors().add(headers);
这一次再去请求,发现校验已经通过了,可以正常拉下来数据了。
可能大家在ip直连的时候也会遇到类似的问题,在这里我贴出自己okhttp的相关代码,希望能有帮助。