2

I'm having trouble reading what cookies are being sent when making a POST in java.

Here's my code:

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();
}

This is what it prints:
Cookies: PHPSESSID=48863f8c3adcbddf0e77e7f1b450fc0e; path=/

This is what I want it to print:
ki_u=68debd85-c1af-f1ff-2e6c-4146755c6e26; ki_t=1354418220596%3B1354418220596%3B1354422379616%3B1%3B36;

Any help please?

Thanks

4

1 回答 1

1

无法以这种方式读取该 cookie。

作为响应,http://www.rune...17648.html您只会PHPSESSID在响应标头中获得 cookie。

您要查找的 cookie (ki_uki_t) 由该文件中的 JavaScript 代码设置:

http://s3.amazonaws.com/ki.js/45645/919.js

因此,要实际获取 cookie 值,您需要复制浏览器行为或实际使用浏览器(请求 html 页面、解析它、下载引用的资源(特别是919.js)并执行 JavaScript 代码)。

于 2012-12-02T04:30:19.160 回答