Handling cookies in Java?

Status
Not open for further replies.

sorghumking

Ars Scholae Palatinae
989
The Scenario:
Some friends and I are in a Yahoo fantasy football league. I'm trying to write a Java app that will periodically glean our stats from pertinent pages and spit them out in a nice format (Yahoo actually has something that does this, called StatTracker, but it costs $8, and I thought writing one might be a fun diversion). The problem is that, because our league is "private", the pages are accessible only if I'm logged in.

So I POST my login information (in addition to the numerous hidden fields Yahoo desires), and the page returned says "The browser you're using refuses to sign in. (cookies rejected)".

The Question:
How the hell do I "accept" cookies with my ultra-stripped-down "browser"?

My understanding of the login process and cookies' relation to it is as follows:

1) cookie-free user goes to Yahoo login page and enters login info, then submits it
2) server inspects login info
3) if info is kosher, server returns some sort of "way to go, you logged in!" page and includes a cookie in the HTTP header
4) user's browser grabs this cookie and, on subsequent page requests made to this domain, includes the cookie, which allows the server to verify that the requesting user is kosher
5) the server may send a new cookie back on subsequent page requests, with which the browser simply replaces the previous cookie

...and repeat 5) indefinitely.

What do I have wrong here? Does anyone understand the intricacies of the freaking Yahoo login process?
 

ssmith

Ars Scholae Palatinae
928
You can get the 'Set-Cookie' header from the HttpURLConnection after the post. Parse the cookie names and values.
In subsequent requests, use the setRequestProperty method to send the cookies back up (semicolon delimited).
The problem may be more subtle though, if it is failing on the first POST you make, it sounds like it is expecting that you already have the cookie.
 
Status
Not open for further replies.