httpcomponents的奇怪行为

时间:2012-10-31 13:36:45

标签: java cookies httpclient apache-httpcomponents

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

0 个答案:

没有答案