我正在为客户开发应用程序。网站是https://www.seekingarrangement.com/login
当我使用 Chrome 或 Firefox 时,我看到“Set-Cookie”的响应 header ,其值为 “laravel_session=eyJpdiI6InlXNkxTRzUxODRtR3BkT0xIR3ZDd0E9PSIsInZhbHVlIjoiMzlRaDdTSk1IRnNINjAyaWVvaGURN25DYlJaRDVTNDVoMXBLWkdjaWNC N25Ldzg1UKVZazJ0c1JIVFhxODlMSytaRlk5Skd6Z0EyRG9XK29SUWhtOXc9PSIsIm1hYyI6ImFiNjdiYTczNDE4ZGMyOWViOTcxYTljZmUyNjhjY2ViM2E3N2ViMTBiMzBlYjA4MzgwZjhjOTVhNmV mMGM0OTMifQ%3D%3D;过期=2016 年 4 月 2 日星期六 04:19 :15 GMT; Max-Age=82800; path=/; httponly
但是,当我运行以下代码时,“Set-Cookie”值为空。
private void onLoad() {
CookieHandler.setDefault(new CookieManager());
login();
}
private void login() {
try {
System.out.println("Logging in...");
this.email = emailField.getText();
this.password = passField.getText();
/* 1. connect to login url */
URL URL = new URL("https://www.seekingarrangement.com/login");
HttpURLConnection conn = (HttpURLConnection) URL.openConnection();
/* 2. GET response string */
conn.setRequestMethod("GET");
/* 3. Get Response cookies */
Map cookiesTest = conn.getHeaderFields();
String headerKeys[] = new String[cookiesTest.size()];
String headerValues[] = new String[cookiesTest.size()];
for(int i = 0;i<cookiesTest.size(); i++) {
headerKeys[i] = conn.getHeaderFieldKey(i);
headerValues[i] = conn.getHeaderField(i);
System.out.println(i + ". " + headerKeys[i] + " : " + headerValues[i]);
}
// more code no relevant to my question
}
这给我留下了控制台的以下输出:
Logging in...
0. null : HTTP/1.1 200 OK
1. Server : cloudflare-nginx
2. Date : Fri, 01 Apr 2016 05:37:35 GMT
3. Content-Type : text/html; charset=UTF-8
4. Transfer-Encoding : chunked
5. Connection : keep-alive
6. Set-Cookie :
7. X-Powered-By : PHP/5.6.14
8. Cache-Control : no-cache
9. Set-Cookie :
正如您所看到的,两个“Set-Cookie”响应 header 都是空白的。但是如果我检查任何浏览器的开发人员工具,它会显示“Set-Cookie”的实际值
最佳答案
如果删除以下行
CookieHandler.setDefault(new CookieManager());
它应该在您的系统输出中显示 Set-Cookie
header 。
或者,如果您使用CookieManager
,您应该能够看到存储在CookieStore
中的cookie,如下所示:
CookieManager manager = new CookieManager();
CookieHandler.setDefault(manager);
并且您可以在cookie存储中查看cookie:
if(manager.getCookieStore().getCookies().size() > 0)
{
System.out.println("From cookieManager : " + manager.getCookieStore().getCookies());
}
关于Java 为什么没有读取 "Set-Cookie" header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36348891/