我正在用okhttp登录一个网站。通过浏览器检查,我知道最初的响应是302重定向,我应该得到4个cookie:
但是,到目前为止,我试图显示收到的cookie,这表明响应为200,并且只打印jsession id cookie。
// COOKIE HANDLER
CookieHandler cookieHandler = new CookieManager();
// INTERCEPTOR
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
// PROXY
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
// NEW CLIENT
OkHttpClient client = new OkHttpClient.Builder()
.proxy(proxy)
.proxyAuthenticator(proxyAuthenticator)
.cookieJar(new JavaNetCookieJar(cookieHandler))
.addInterceptor(logging)
.build();
RequestBody formBody = new FormBody.Builder()
.add("user", "pass")
.build();
Request request = new Request.Builder()
.url("https://www.contoso.com")
.post(formBody)
.build();
Response response = client.newCall(request).execute();
System.out.println(response.code());
Headers headers = response.headers();
for (int i =0, size = headers.size(); i < size; i++ ) {
if (headers.name(i).equals("Set-Cookie") ) {
System.out.print("Found a cookie:" + headers.value(i));
}
重定向是否导致我看不到所有的cookie,是否有方法捕获所有cookie?我想这和302有关。
如何检查cookiehandler中的内容?
1条答案
按热度按时间rqdpfwrv1#
尝试在cookiejar上调用loadforrequest。这将为您提供它所知道的目标url的所有cookies。