【问题标题】:retrofit okhttpclient with CertificatePinner使用 CertificatePinner 改造 okhttpclient
【发布时间】:2016-04-01 02:48:51
【问题描述】:

我正在使用带有 OkHttp 3.0.0-RC1 的 Retrofit 2.0.0-beta3 并面临以下微不足道的问题。我连接到的 HTTPS 服务器可以正常工作,我使用 CertificatePinner 和 OkHttp 调用,但不能使用 Retrofit 调用,即使我在 Retrofit 实例中设置相同的客户端

String hostname = "hostname";
CertificatePinner certificatePinner = new CertificatePinner.Builder()
        .add(hostname, "sha1/rNKiM/IsTzTMJ09jpMtPq4qP+Q8=")
        .add(hostname, "sha1/hL8+j9RH89wlAW7eNDSS1ZlZ8Z8=")
        .build();
OkHttpClient client = new OkHttpClient.Builder().certificatePinner(certificatePinner).build();

// This call works
OkHttpClient client = new OkHttpClient.Builder().certificatePinner(certificatePinner).build();
Request request = new Request.Builder()
    .url("https://" + hostname + "/api/me")
    .addHeader("Authorization", "Bearer token")
    .build();
okhttp3.Call call = client.newCall(request);
//execute call returns 200 with response

// This is not working and throwing SSL Connection Error
retrofit = new Retrofit.Builder()
        .baseUrl("https://" + hostname)
        .addConverterFactory(GsonConverterFactory.create(gson))
        .client(client)
        .build();

【问题讨论】:

  • “不起作用”是什么意思?
  • @Robert 现在已修复,我在执行顺序中发现了一个错误。我已经把我的答案放在下面了

标签: android retrofit okhttp retrofit2 okhttp3


【解决方案1】:

这是应用程序中的一个错误,因此客户端是在以下代码之前创建的:

ProviderInstaller.installIfNeeded(getApplication());

现在订单已修复,代码运行正常。

【讨论】:

    猜你喜欢
    • 2023-02-07
    • 2016-11-30
    • 1970-01-01
    • 2018-02-04
    • 2021-02-13
    • 2022-01-24
    • 1970-01-01
    • 2019-03-01
    • 2017-07-06
    相关资源
    最近更新 更多