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
To send cookies to the server, you need to add the "Cookie: name=value" header to your request. To send multiple Cookies in one cookie header, you can separate them with semicolons. In this Send Cookies example, we are sending HTTP cookies to the ReqBin echo URL.
It worked after adding
cookie.setDomain(".xyz.net");
cookie.setPath("/");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With