【问题标题】:retrofit 2 header doesn't work改造 2 标头不起作用
【发布时间】:2017-06-27 18:35:01
【问题描述】:

我有在其响应中返回 sessionId 的登录 API,我必须为 API 发送这个 sessionId,该 API 在标头中使用 sessionId 并返回用户是否之前登录过,但它总是在 android 中返回 false 值。但我使用邮递员对其进行了测试-启用了拦截器-并且效果很好。

cookie 正常出现在日志中

这就是我所做的

public class CookieInterceptor implements Interceptor {
    private static volatile String cookie;

    public static void setSessionCookie(String cookies) {
        cookie = cookies;
    }

    @Override
    public okhttp3.Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        if (cookie != null) {
            request = request.newBuilder()
                    .addHeader("Cookie", cookie)
                    .build();
        }
        return chain.proceed(request);
    }
}

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);

        final OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addNetworkInterceptor(logging)
                .addInterceptor(new CookieInterceptor())
                .readTimeout(120, TimeUnit.SECONDS)
                .connectTimeout(120, TimeUnit.SECONDS)
                .build();
        mRetrofit = new Retrofit.Builder()
                .baseUrl(Constants.BASE_URL)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxErrorHandlingCallAdapterFactory.create())
                .build();

日志是: - 对于已发送的请求

--> GET https://giftdisk.com/my_en/mobileapi/customer/status http/1.1
 D/OkHttp: Cookie: frontend=d6652f83347c30f500cd444191b7cefb
 D/OkHttp: Host: giftdisk.com
 D/OkHttp: Connection: Keep-Alive
 D/OkHttp: Accept-Encoding: gzip
 D/OkHttp: User-Agent: okhttp/3.3.1
 D/OkHttp: --> END GET
  • 对于收到的请求

    <-- 200 OK https://giftdisk.com/my_en/mobileapi/customer/status (728ms)
     D/OkHttp: Date: Thu, 09 Feb 2017 16:08:45 GMT
     D/OkHttp: X-Powered-By: PHP/5.4.45
     D/OkHttp: Expires: Thu, 19 Nov 1981 08:52:00 GMT
     D/OkHttp: Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
     D/OkHttp: Pragma: no-cache
     D/OkHttp: Content-Encoding: gzip
     D/OkHttp: Vary: Accept-Encoding
     D/OkHttp: Set-Cookie: frontend=dadcf55472df1475a63a965e8363c914; expires=Thu, 09-Feb-2017 17:08:46 GMT; path=/; domain=giftdisk.com;
        02-10 06:13:32.859 31752-32731/ae.cws.giftdisk D/OkHttp: Set-Cookie: frontend_cid=j1JGqUiVmox6GjQ6; expires=Thu, 09-Feb-2017 17:08:46 GMT; path=/; domain=giftdisk.com; secure; httponly
     D/OkHttp: Keep-Alive: timeout=3, max=30
     D/OkHttp: Connection: Keep-Alive
     D/OkHttp: Transfer-Encoding: chunked
     D/OkHttp: Content-Type: text/html; charset=UTF-8
     D/OkHttp: <-- END HTTP (encoded body omitted)
    

【问题讨论】:

    标签: android retrofit2 okhttp3


    【解决方案1】:

    听起来您可能想通过CookieManager 使用本机cookie jar / 策略支持。请参阅其他 SO 问题/答案:https://stackoverflow.com/a/24267060/25398

    【讨论】:

      【解决方案2】:

      您的日志的第二部分是服务器实际响应的内容,这可能与您发送的内容(第一个日志)完全无关。

      你在期待什么?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-26
        • 2016-10-06
        • 2018-04-02
        • 2018-12-16
        相关资源
        最近更新 更多