aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes handling cookies with URLConnection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "handling cookies with URLConnection" Watch "handling cookies with URLConnection" New topic
Author

handling cookies with URLConnection

Dmitri Christo
Ranch Hand

Joined: Jan 19, 2007
Posts: 81
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

Joined: Oct 29, 2001
Posts: 8927
    
    9

I use Jakarta HTTP Client, which handles cookies much more gracefully than URLConnection.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Dmitri Christo
Ranch Hand

Joined: Jan 19, 2007
Posts: 81
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

Joined: Oct 29, 2001
Posts: 8927
    
    9

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

Joined: Jan 19, 2007
Posts: 81
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 ]
 
jQuery in Action, 2nd edition
 
subject: handling cookies with URLConnection