改造不通过 h​​ttps 发送 headers

retrofit not sending the headers over https

我一直在 "HTTP" 上使用改造并且工作完美直到我切换到 "HTTPS" 突然服务器停止能够看到 headers 或者改造不能发送它。

OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor() {

           @Override
           public Response intercept(Chain chain) throws IOException {
                        Request newRequest = chain.request().newBuilder()
                                .addHeader(Constants.AccessTokenHeaderName, AccessToken)
                                .addHeader(Constants.ClientTokenHeaderName, Global.getClientToken())
                                .addHeader(Constants.AuthCodeHeaderName, Global.getAuthCode(context))
                                .build();
                        return chain.proceed(newRequest);
                    }
                }).connectTimeout(0, TimeUnit.SECONDS).build();
                Gson gson = new GsonBuilder()
                        .setLenient()
                        .create();
                retrofitWithAuth = new Retrofit.Builder()
                        .client(client)
                        .baseUrl(BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create(gson)).build();

也许您错过了 协议(Collections.singletonList(协议。HTTP_1_1))。 我以这种方式创建了 OkHttpClient:

 private static OkHttpClient getNewClient() {
    Interceptor interceptor = new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            //todo
            return null;
        }
    };
    return new OkHttpClient.Builder()
            .addInterceptor(interceptor)
            .connectTimeout(10, TimeUnit.SECONDS)
            .writeTimeout(10, TimeUnit.SECONDS)
            .readTimeout(20, TimeUnit.SECONDS)
            .protocols(Collections.singletonList(Protocol.HTTP_1_1)) // Disable HTTP/2 for interop with NGINX 1.9.5.
            .build();
}