英文:
why cookie is not loaded for subsequent requests?
问题
我正在使用 okhttp 4.9.0
private OkHttpClient http;
public SonarApi(String serverUrl, String secret) {
HttpLoggingInterceptor it = new HttpLoggingInterceptor();
it.setLevel(Level.BODY);
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
JavaNetCookieJar cookieJar = new JavaNetCookieJar(cookieManager);
http = new OkHttpClient.Builder().addInterceptor(it).cookieJar(cookieJar).build();
}
public void login(String userName, String password) throws IOException {
RequestBody formBody = new FormBody.Builder().add("login", userName).add("password", password).build();
Request req = new Request.Builder()
.addHeader("Accept", "application/json")
.addHeader("Host", "x.x.x.x:9876")
.addHeader("Origin", serverUrl)
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.url(serverUrl + "/api/authentication/login").post(formBody).build();
http.newCall(req).execute();
}
public boolean updateRuleMarkdownNote(String ruleKey, String mdNote) throws IOException {
RequestBody formBody = new FormBody.Builder().add("key", ruleKey).add("markdown_note", mdNote).build();
Request req = new Request.Builder().addHeader("Content-Type", "application/x-www-form-urlencoded").url(serverUrl + "/api/rules/update").post(formBody).build();
int code = http.newCall(req).execute().code();
return code >= 200 && code < 300;
}
public static void main(String[] args) {
SonarApi sonarApi = new SonarApi("http://x.x.x.x:9876", "cac3b3c65347e87cdf1f9a7352935db79e2435f5");
try {
sonarApi.login("admin", "******");
sonarApi.updateRuleMarkdownNote("custom-rules-java:ExampleRules01", "*foo*");
} catch (IOException e) {
e.printStackTrace();
}
}
日志:
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: --> POST http://172.25.160.238:9876/api/authentication/login
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Content-Length: 34
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Accept: application/json
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Host: 172.25.160.238:9876
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Origin: http://172.25.160.238:9876
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Content-Type: application/x-www-form-urlencoded
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息:
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: login=admin&password=%23Fgglgy0223
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: --> END POST (34-byte body)
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: <-- 200 http://172.25.160.238:9876/api/authentication/login (266ms)
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: X-Frame-Options: SAMEORIGIN
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: X-XSS-Protection: 1; mode=block
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: X-Content-Type-Options: nosniff
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Set-Cookie: XSRF-TOKEN=5iohli0jdhtla6tk8a87598log; Max-Age=259200; Expires=Mon, 19-Jun-2023 06:44:23 GMT; Path=/
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Set-Cookie: JWT-SESSION=eyJhbGciOiJIUzI1NiJ9.eyJsYXN0UmVmcmVzaFRpbWUiOjE2ODY4OTc4NjMyNTMsInhzcmZUb2tlbiI6IjVpb2hsaTBqZgh0bGE2dGs4YTg3NTk4bG9nIiwianRpIjoiQVlqQzhxcFNkRWtsQ2VJdXhIQ0IiLCJzdWIiOiJBWHd3MUhN5ncNCN0xfa1UtMEhydyIsImlhdCI6MTY4Njg5Nzg2MywiZXhwIjoxNjg3MTU3MDYzfQ.sDbWX2_pNGIAIMjrgOBSyEnsBLc9fncz2-7XS_uyx-M; Max-Age=259200; Expires=Mon, 19-Jun-2023 06:44:23 GMT; Path=/; HttpOnly
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Content-Length: 0
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Date: Fri, 16 Jun 2023 06:44:23 GMT
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Keep-Alive: timeout=60
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Connection: keep-alive
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: <-- END HTTP (0-byte body)
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: --> POST http://172.25.160.238:9876/api/rules/update
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Content-Length: 58
六月 16, 2023 2:
<details>
<summary>英文:</summary>
I am using okhttp 4.9.0
```java
private OkHttpClient http;
public SonarApi(String serverUrl, String secret) {
HttpLoggingInterceptor it = new HttpLoggingInterceptor();
it.setLevel(Level.BODY);
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
JavaNetCookieJar cookieJar = new JavaNetCookieJar(cookieManager);
http = new OkHttpClient.Builder().addInterceptor(it).cookieJar(cookieJar).build();
}
public void login(String userName, String password) throws IOException {
RequestBody formBody = new FormBody.Builder().add("login", userName).add("password", password).build();
Request req = new Request.Builder()
.addHeader("Accept", "application/json")
.addHeader("Host", "x.x.x.x:9876")
.addHeader("Origin", serverUrl)
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.url(serverUrl + "/api/authentication/login").post(formBody).build();
http.newCall(req).execute();
}
public boolean updateRuleMarkdownNote(String ruleKey, String mdNote) throws IOException {
RequestBody formBody = new FormBody.Builder().add("key", ruleKey).add("markdown_note", mdNote).build();
Request req = new Request.Builder().addHeader("Content-Type", "application/x-www-form-urlencoded").url(serverUrl + "/api/rules/update").post(formBody).build();
int code = http.newCall(req).execute().code();
return code >= 200 && code < 300;
}
public static void main(String[] args) {
SonarApi sonarApi = new SonarApi("http://x.x.x.x:9876", "cac3b3c65347e87cdf1f9a7352935db79e2435f5");
try {
sonarApi.login("admin", "******");
sonarApi.updateRuleMarkdownNote("custom-rules-java:ExampleRules01", "*foo*");
} catch (IOException e) {
e.printStackTrace();
}
}
logs:
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: --> POST http://172.25.160.238:9876/api/authentication/login
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Content-Length: 34
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Accept: application/json
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Host: 172.25.160.238:9876
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Origin: http://172.25.160.238:9876
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Content-Type: application/x-www-form-urlencoded
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息:
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: login=admin&password=%23Fgglgy0223
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: --> END POST (34-byte body)
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: <-- 200 http://172.25.160.238:9876/api/authentication/login (266ms)
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: X-Frame-Options: SAMEORIGIN
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: X-XSS-Protection: 1; mode=block
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: X-Content-Type-Options: nosniff
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Set-Cookie: XSRF-TOKEN=5iohli0jdhtla6tk8a87598log; Max-Age=259200; Expires=Mon, 19-Jun-2023 06:44:23 GMT; Path=/
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Set-Cookie: JWT-SESSION=eyJhbGciOiJIUzI1NiJ9.eyJsYXN0UmVmcmVzaFRpbWUiOjE2ODY4OTc4NjMyNTMsInhzcmZUb2tlbiI6IjVpb2hsaTBqZGh0bGE2dGs4YTg3NTk4bG9nIiwianRpIjoiQVlqQzhxcFNkRWtsQ2VJdXhIQ0IiLCJzdWIiOiJBWHd3MUhOY1NCN0xfa1UtMEhydyIsImlhdCI6MTY4Njg5Nzg2MywiZXhwIjoxNjg3MTU3MDYzfQ.sDbWX2_pNGIAIMjrgOBSyEnsBLc9fncz2-7XS_uyx-M; Max-Age=259200; Expires=Mon, 19-Jun-2023 06:44:23 GMT; Path=/; HttpOnly
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Content-Length: 0
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Date: Fri, 16 Jun 2023 06:44:23 GMT
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Keep-Alive: timeout=60
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Connection: keep-alive
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: <-- END HTTP (0-byte body)
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: --> POST http://172.25.160.238:9876/api/rules/update
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Content-Length: 58
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Content-Type: application/x-www-form-urlencoded
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息:
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: key=custom-rules-java%3AExampleRules01&markdown_note=*foo*
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: --> END POST (58-byte body)
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: <-- 401 http://172.25.160.238:9876/api/rules/update (7ms)
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: X-Frame-Options: SAMEORIGIN
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: X-XSS-Protection: 1; mode=block
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: X-Content-Type-Options: nosniff
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Content-Length: 0
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Date: Fri, 16 Jun 2023 06:44:23 GMT
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Keep-Alive: timeout=60
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: Connection: keep-alive
六月 16, 2023 2:42:54 下午 okhttp3.internal.platform.Platform log
信息: <-- END HTTP (0-byte body)
after login, I can see setcookie header from server, but why cookies not loaded for subsequent requests? I can't figure it out why. as you can see in the logs, login request is successful and cookies named as XSRF-TOKEN
and JWT-SESSION
was returned within response headers, but the following request did not send a Cookie
header with them.
答案1
得分: 0
HttpLoggingInterceptor
没有打印来自CookieJar
的cookie,因为它在内部拦截器加载来自CookieJar
的cookie之前执行。
通过使用tcpdump,我可以看到后续请求的Cookie头。
案件已关闭。
英文:
HttpLoggingInterceptor
didn't print cookies from CookieJar
, because it was executed before the internal Interceptor which load cookie from CookieJar
By using tcpdump, I can see Cookie headers for subsequent requests.
case closed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论