7

我试图在发送 http 请求之前设置 abc=123 cookie。

在响应中,我希望将相同的 cookie 发回。但是在响应中,我得到 abc=890 的值是由目标服务器设置的。

        DefaultHttpClient httpclient = new DefaultHttpClient();
    CookieStore cookieStore = httpclient.getCookieStore();
    BasicClientCookie cookie = new BasicClientCookie("abc", "123");

    // Prepare a request object
    HttpGet httpget = new HttpGet("http://abc.net/restofurl");

    cookieStore.addCookie(cookie);
    httpclient.setCookieStore(cookieStore);

    // Execute the request
    HttpResponse response = httpclient.execute(httpget);

    // Examine the response status
    log.info("Http request response is: " + response.getStatusLine());

    List<Cookie> cookies = cookieStore.getCookies();

    for (int i=0; i<cookies.size();i++) {

        if (cookies.get(i).getName().toString().equals("abc")) {
            log.info("cookie is: " + cookies.get(0).getValue().toString());
            }
    }

谢谢

4

2 回答 2

8

添加后它工作

cookie.setDomain(".xyz.net");
cookie.setPath("/");
于 2013-01-02T15:07:27.497 回答
0

问题是否通过更改解决

log.info("cookie is: " + cookies.get(0).getValue().toString());

进入

log.info("cookie is: " + cookies.get(i).getValue().toString());

?

于 2013-01-02T01:24:39.357 回答