• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

handling cookies with URLConnection

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
After some pecan pie, you might want to cleanse your palatte with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic