46

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?

Yoon5oo
  • 496
  • 5
  • 11
hitan
  • 591
  • 1
  • 4
  • 7

4 Answers4

43

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!

nemo
  • 485
  • 1
  • 3
  • 15
Keith Lee
  • 431
  • 4
  • 2
  • 13
    The key for me was to set the domain AND the path. It wouldn't pass the cookie to the server without it. – christophercotton May 23 '12 at 15:56
  • 1
    I spent many times to solve issue with cookies, server didn't want accept my request, however firebug and chrome dev tools shows that all cookien parameters except value are empty. Only when I set domain and path values, request was success. Thank you. – Georgy Gobozov Jun 19 '13 at 13:53
  • 1
    This approach is now deprecated since AbstractHttpClient, which exposes the setCookieStore method, is now deprecated as of release 4.3. @khai's approach is now the preferred one. – John Rix Mar 11 '15 at 15:35
  • Update from the future: for apache http client >=4.5, you need also [this](https://stackoverflow.com/a/34396477/1783163) answer. – peterh Jul 24 '18 at 07:10
23

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);
Sastrija
  • 3,284
  • 6
  • 47
  • 64
khai
  • 231
  • 2
  • 3
  • 2
    You should use HttpClientContext instead of ClientContext which is now marked as deprecated. – Bludwarf Oct 30 '15 at 16:19
  • 2
    Update from the future: for apache http client >=4.5, you need also [this](https://stackoverflow.com/a/34396477/1783163) answer. – peterh Jul 24 '18 at 07:10
11
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");
liliya
  • 251
  • 3
  • 6
  • 1
    I also found this answer in another topic, really, it should set domain. – CoolMind Jun 03 '16 at 14:56
  • newer versions i struggled to find the constant, you can also use `weblabCookie.setAttribute("domain", "true")`. it is mandatory otherwise the cookie will be silently ignore. exceedingly bad design if you ask me but what can you do – Asad-ullah Khan Aug 11 '23 at 18:44
2

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

Mi-Creativity
  • 9,554
  • 10
  • 38
  • 47
  • 1
    Welcome to this site! This is a Q from 2010, with two answers that have a bunch of upvotes. I am not sure if extra, imprecise hints would help anyone. – joepd Dec 16 '15 at 23:14