do you know me how you can store jsessionid in cookie, so it may be passed towards the servlet with publish request? I am using Apache HttpClient version 4..3. All of the solutions I have found describes how to get this done with HttpClient 3.1. I have read tutorial and attempted this, however it is not 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 am hooking up to servlets compiled by friend. I have drenched in and acquired jsessionid
. Now I wish to send another request and want to pass through jsessionid for authorization purpose.
Servlet works fine because I made use of java HttpURLConnection, set the cookie, passed it also it labored. With HttpClient I recieve no exceptions however the return code from friend's servlet signifies that there is no sessionid within the request.
Another Edit - I have got one solution
I set the parameter of request header also it labored. Servlet recognized sessionid.
httppost.setHeader("Cookie", "JSESSIONID="+ getSessionId());
Now my real question is: Is method correct?
Used to do it by passing the cookie with the HttpContext:
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
response = client.execute(httppost, localContext);
I'm so glad to resolve this issue:
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);
Very Easy!