7

I am trying to set abc=123 cookie before sending http request.

In the response I am expecting the same cookie to be sent back. But in the response I get abc=890 where the value is set by the target server.

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

Thanks

2 Answers 2

8

It worked after adding

cookie.setDomain(".xyz.net");
cookie.setPath("/");
Sign up to request clarification or add additional context in comments.

Comments

0

Is the problem resolved by changing

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

into

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

?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.