3

I'm using a URLConnection to login to a page. When I successfully login a session value will be set on the page. After that I want to access an other file on the site, but I can't maintain the session state of the site. Any ideas?

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
user1156691
  • 233
  • 1
  • 4
  • 11

3 Answers3

2

I suggest that you use Apache HttpClient / HttpComponents instead. It has facilities for maintaining a client-side cookie store.


Maintaining session state across URLConnection instances involves:

  • getting the set-cookie response headers
  • parsing them, figuring out what they apply to, and storing them
  • creating and adding cookie request headers for follow-on requests.

Prior to Java 1.6, there were no public Java APIs to do this for you and you had to do it all "by hand". Starting with Java 1.6, there is support in the form of CookieHandler / CookieManager / HttpCookie / CookieStore / CookiePolicy. Refer to the javadocs for details.

Related pages:

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

It sounds like the website you are trying to access handles sessions based on cookies. You may need to capture that cookie and add it to future requests. This question may help with that piece:

URLConnection with Cookies?

Community
  • 1
  • 1
Ulises
  • 13,229
  • 5
  • 34
  • 50
0

Read from the URLConnection to see if any cookies are set or a redirect is sent that contains a session id you can send back to the other site.

Skip Head
  • 7,580
  • 1
  • 30
  • 34