在使用java进行POST时,我无法读取正在发送的cookie。
这是我的代码:
public static void main(String[] args) throws MalformedURLException, IOException {
String urlParameters = "votebut=";
String request = "http://www.runelocus.com/toplist/vote-17648.html";
URL url = new URL(request);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
connection.setUseCaches(false);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
List<String> cookies = connection.getHeaderFields().get("Set-Cookie");
for (String cookie : cookies) {
System.out.println("Cookies: " + cookie);
}
connection.disconnect();
}
这是它打印的内容:
Cookies: PHPSESSID=48863f8c3adcbddf0e77e7f1b450fc0e; path=/
这就是我想要打印的内容:
ki_u=68debd85-c1af-f1ff-2e6c-4146755c6e26; ki_t=1354418220596%3B1354418220596%3B1354422379616%3B1%3B36;
请帮忙吗?
由于
答案 0 :(得分:1)
无法以这种方式读取cookie。
在回复http://www.rune...17648.html
时,您只能在回复标题中获得PHPSESSID
个Cookie。
您要查找的Cookie(ki_u
和ki_t
)由此文件中的JavaScript代码设置:
http://s3.amazonaws.com/ki.js/45645/919.js
因此,要实际获取cookie值,您需要复制浏览器行为或实际使用浏览器(请求html页面,解析它,下载引用的资源(特别是919.js
)并执行JavaScript代码)。