gpt4 book ai didi

OkHttp3 日志拦截器不记录 cookie

转载 作者:行者123 更新时间:2023-12-04 14:41:21 32 4
gpt4 key购买 nike

我正在使用 OkHttp 3(带有改造 2),并且我正在尝试使 cookie 和日志记录工作。问题是日志显示了所有内容,但没有显示 cookie。我已经捕获了 HTTP 流量并且正在发送 cookie,只是没有记录它们。我尝试调试日志拦截器,确实,当请求到达 intercept() 时,它似乎没有 cookie(尽管它确实有其他自定义 header )。称呼。任何想法我做错了什么?

    final Gson gson = new GsonBuilder()
.setDateFormat(JSON_DATE_FORMAT)
.create();

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);

OkHttpClient.Builder okClientBuilder = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain
.request()
.newBuilder()
.addHeader("custom-header", Utils.getRandomUUIDForHeader())
.build();
return chain.proceed(request);
}
})
.cookieJar(new JavaNetCookieJar(cookieManager))
.addInterceptor(logging);

if (mock) {
okClientBuilder.addInterceptor(new MockApiInterceptor(context));
}

return new Retrofit
.Builder()
.baseUrl(serverURL)
.client(okClientBuilder.build())
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(DefaultApi.class);

示例输出:
<-- 200 OK http://localhost:53580/login (31ms)
Content-Length: 66
Set-Cookie: JSESSIONID=4b42fc452e6adc55334e65b945c24b8886b127a57786a6cd51de6b3117a58cc9; Path=/
OkHttp-Sent-Millis: 1467298601713
OkHttp-Received-Millis: 1467298601718

{
"status":"OK",
"sessionId":"p8tvdIHxQaON",
"entities":[]
}
<-- END HTTP (66-byte body)
--> GET http://localhost:53580/data http/1.1
custom-header: 663d0354-8a16-471a-bf6d-fb7fdb3d3404
--> END GET

最佳答案

日志拦截器在 cookie 添加到请求之前运行。通过使用网络拦截器而不是应用程序拦截器来解决此问题。更多指南在这里:

https://github.com/square/okhttp/wiki/Interceptors

关于OkHttp3 日志拦截器不记录 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38127107/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com