Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

handling cookies with URLConnection

 
Dmitri Christo
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am trying to understand how cookies should be handled over the same http session. I know how to query and store cookies and then send cookies back in a POST request but cannot do that in the same session.

Setting the cookie with setRequestProperty() must be done before calling connect(), but querying and storing the cookie with getHeaderField() is possible only after connect() is called. So, how can I send a cookie parameter before even knowing its value?

The trouble here is that with every connection the cookie value changes to reflect a new session id. If it was a cookie with a fixed/static value, I could do this in a two-step process. But this is not the case here..

Hope someone might have faced something similar..

Many thanks for any help.
[ May 20, 2008: Message edited by: Dmitri Christo ]
 
Joe Ess
Bartender
Posts: 9280
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use Jakarta HTTP Client, which handles cookies much more gracefully than URLConnection.
 
Dmitri Christo
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, HttpClient works smoother, but I have to user URLConnection in my case.

There is a lot of information about URLConnection and cookies out there, but couldn't find an answer to this one.

I am trying to understand the lifecycle of the session, so I can use the cookie in my response. So the connection is established with myUrl.openConnection(); - and it is opened with myUrl.connect(); right?

I have to set the cookie with myUrl.setRequestProperty("Cookie", myCookie); before calling connect(), but it is too soon because to find out the cookie value, I have to call getHeaderField(); after connect() -- Any ideas?
 
Joe Ess
Bartender
Posts: 9280
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dmitri Christo:
I have to set the cookie with myUrl.setRequestProperty("Cookie", myCookie); before calling connect(), but it is too soon because to find out the cookie value, I have to call getHeaderField(); after connect() -- Any ideas?


Wouldn't you want to set the cookie value from the previous request in the current one, not attempt to read the response the current request?
 
Dmitri Christo
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure what is wrong with my code. My question is, am I making one or two distinct requests/connections here? I can tell I am carrying the same cookie over to the second urlconnection, but I am still getting this response (when I retrieve and print the html response from the server):


Here is my approach:


--Am I making an obvious mistake I am not seeing, or could it be something with redirects, etc?

Thanks for any help
[ May 21, 2008: Message edited by: Dmitri Christo ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic