Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache HttpClient 4.0.3 - how do I set cookie with sessionID for POST request?

Can you tell me how to store jsessionid in the cookie, so it can be passed to the servlet with post request? I'm using Apache HttpClient version 4.0.3. All the solutions I've found explains how to do this with HttpClient 3.1. I've read the tutorial and tried this, but it isn't working.

HttpPost httppost = new HttpPost(postData);
CookieStore cookieStore = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", getSessionId());
cookieStore.addCookie(cookie);
client.setCookieStore(cookieStore);
response = client.execute(httppost);

Edit - further explanations
I'm connecting to servlets written by a friend. I've logged in and obtained jsessionid. Now I want to send another request and need to pass jsessionid for authorization purposes. Servlet works fine because I used java HttpURLConnection, set the cookie, passed it and it worked. Now with HttpClient I get no exceptions but the return code from a friend's servlet indicates that there was no sessionid in the request.

Another Edit - I've got one solution I set the parameter of request header and it worked. Servlet recognized sessionid.
httppost.setHeader("Cookie", "JSESSIONID="+ getSessionId());

Now my question is: Is this method correct?

like image 518
hitan Avatar asked Nov 12 '10 15:11

hitan


People also ask

How do I get the HTTP response of a cookie?

Use the HTTP request object that is passed to the service method of your servlet to get all cookies that the client browser sent with the request. 1. Use the getCookies() method of the HttpServletRequest to retrieve an array of all cookies in the request.


4 Answers

I am so glad to solve this problem:

HttpPost httppost = new HttpPost(postData); 
CookieStore cookieStore = new BasicCookieStore(); 
BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", getSessionId());

//cookie.setDomain("your domain");
cookie.setPath("/");

cookieStore.addCookie(cookie); 
client.setCookieStore(cookieStore); 
response = client.execute(httppost); 

So Easy!

like image 68
Keith Lee Avatar answered Oct 01 '22 17:10

Keith Lee


I did it by passing the cookie through the HttpContext:

HttpContext localContext = new BasicHttpContext();

localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

response = client.execute(httppost, localContext);
like image 30
khai Avatar answered Oct 04 '22 17:10

khai


HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
response = client.execute(httppost, localContext);

doesn't work in 4.5 version without

cookie.setDomain(".domain.com");
cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "true");
like image 32
liliya Avatar answered Oct 03 '22 17:10

liliya


You should probably set all of the cookie properties not just the value of it. setPath(), setDomain() ... etc

like image 38
Ramdane Oualitsen Avatar answered Oct 04 '22 17:10

Ramdane Oualitsen