首页 > 解决方案 > 使用 OkHttpClient 连接到 API 导致 TLS 错误

问题描述

我在尝试使用 OkHttpClient 连接到 APi 服务器时遇到错误,对此进行了充分的研究并尝试了任何可能有帮助的选项

我正在使用 Eclipse,我的错误是“连接被对等方关闭”,我无法解决它,所以我下载了 OkHttp 的 jar 文件,然后我发现了错误:

Unable to find acceptable protocols. isFallback=false, modes=[ConnectionSpec(cipherSuites=[TLS_DHE_RSA_WITH_AES_256_CBC_SHA], tlsVersions=[TLS_1_2], supportsTlsExtensions=true)], supported protocols=[SSLv3, TLSv1]

……

String r = "";
Response response;
try {
String url = "https://api.honeywell.com/oauth2/token";
        RequestBody requestBody = new 
    MultipartBody.Builder().setType(MultipartBody.FORM)
                    .addFormDataPart("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML").build();

Request request = new Request.Builder().url(url).post(requestBody).build();
        OkHttpClient client = new OkHttpClient();

        ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS).tlsVersions(TlsVersion.TLS_1_2)
                .cipherSuites(CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA).build();

        client = new OkHttpClient.Builder().connectionSpecs(Collections.singletonList(spec)).build();

        try {
            response = client.newCall(request).execute();
            r = response.body().toString();


        } catch (IOException e) {
            r = "1111 " + e.getMessage();
        }



    } catch (Exception e) {
        r = "222 " + e.getMessage();
    }

    return r;

谁能帮助我如何设置 ConnectioSpecs?我认为这是我的问题,我放在那里的东西我放的东西有误。

感谢您调查我的问题,因为我迷路了。

标签: javaandroidokhttp3

解决方案


推荐阅读