0

我知道 httpcomponents 它不是浏览器,但我只是用它从 Web 服务器中检索一些 cookie。问题是当我发送一个 HTTP 请求时,我没有得到与使用 Firefox 导航时相同的 cookie 集。该网站是 www.amazon.com。我认为这很容易,但我不知道哪个是问题所在。这是我的代码:

    DefaultHttpClient httpclient = new DefaultHttpClient();

    HttpGet httpget = new HttpGet("http://www.amazon.com");

    System.out.println("Initial set of cookies:");
    List<Cookie> cookies = httpclient.getCookieStore().getCookies();
    if (cookies.isEmpty()) {
        System.out.println("None");
    } else {
        for (int i = 0; i < cookies.size(); i++) {
            System.out.println("- " + cookies.get(i).toString());
        }
    }

    HttpResponse response = null;
    try {
        response = httpclient.execute(httpget);
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    HttpEntity entity = response.getEntity();

    System.out.println("Post logon cookies:");
    cookies = httpclient.getCookieStore().getCookies();
    if (cookies.isEmpty()) {
        System.out.println("None");
    } else {
        for (int i = 0; i < cookies.size(); i++) {
            System.out.println("- " + cookies.get(i).toString());
        }
    }
4

0 回答 0