0
我有一个Java方法,它应该获取Set-Cookie属性以便以下登录到网页中。但是conn.getHeaderFields()。get(“Set-Cookie”)不会返回任何内容。有什么建议?无法获得Java中的Set-Cookie属性
private String GetPageContent(String url) throws Exception {
URL obj = new URL(url);
conn = (HttpsURLConnection) obj.openConnection();
// default is GET
conn.setRequestMethod("GET");
conn.setUseCaches(false);
// act like a browser
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setRequestProperty("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
conn.setRequestProperty("Accept-Language", "s-CZ,cs;q=0.8,en;q=0.6");
if (cookies != null) {
for (String cookie : this.cookies) {
conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
}
}
int responseCode = conn.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in =
new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// Get the response cookies
System.out.println(conn.getHeaderFields().get("Set-Cookie")); //print for testing
setCookies(conn.getHeaderFields().get("Set-Cookie"));
return response.toString();
整个程序:http://pastebin.com/3nB682L7
任何.. :-)
您是否有从服务器的原始输出,看到实际发送Set-Cookie?我最近遇到类似的问题,并且cookie被标记为HttpOnly,最近通过URLConnection进行了过滤。 – 2014-12-07 21:39:23
我看到它被标记为HttpOnly ..有没有办法如何复制这个值的设置?:-) – Neonik 2014-12-08 07:28:00
我不知道URLConnection是否可能。您可能必须使用其他HTTP客户端。 – 2014-12-08 07:51:33