3

im making a swing application which will sign in to a server; were im using HttpURLConnection to submit my request and get my response.

problem is when the httpRequest gets to the server the "Cookie: JSESSIONID" header is there, session id is there; but the request.getSession(false) will always return null.

here is the code which i use to set the header on the client:

connection.setRequestProperty("Cookie: JSESSIONID", client.getSessionId());

any help would be apprectiated

Gazaz
  • 167
  • 1
  • 3
  • 9
  • sorry if I misunderstand something, but which value returns from client.getSessionId()? If it was not communicated to you by the server, how can you expect that there is a session existing on the server with this id? The cookie should be given to you by the server, not you communicating to the server what the cookie should be. – John Donn Nov 19 '13 at 08:52
  • the client.getSessionId() will return a session id that was already given by the server. thank you for your time it was a syntax issue and it is solved now :) – Gazaz Nov 19 '13 at 09:28

1 Answers1

4
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); 

See also this Java: How to make a HTTP browsing session and this Apache HttpClient 4.0.3 - how do I set cookie with sessionID for POST request

Community
  • 1
  • 1
StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • 2
    i used http://stackoverflow.com/questions/4166129/apache-httpclient-4-0-3-how-do-i-set-cookie-with-sessionid-for-post-request of your answer and it did solve my problem with "connection.setRequestProperty("Cookie", "JSESSIONID=" + client.getSessionId());" unfortunately it was just a syntax issue :S thank you for your help and time :) – Gazaz Nov 19 '13 at 09:23
  • 1
    @Gazaz Nevertheless this is the correct way to do it. No possibility of syntax errors when you use the API provided for the purpose. – user207421 Nov 19 '13 at 22:01