Java URLConnection problem with Cookies
If there's a site wich Sets a Cookie(a) on the first page, you fill in a form and you need to send that same Cookie(a) to the second page. On this second page you fill another form which sends you to the third page, and if you send Cookie(a), the site will Set another Cookie(b), and then redirects you to the last page. But in order for the last page to show up you need Both the Cookies (a and b).
Now in java if you open a开发者_Python百科 connection to the first page, take the Cookie(a) from the response header and add it as requestProperty to the second page, you can't URLConnection.write() anymore, since you are already connected. This writing of new POSTDATA is necessary to fill in page 2 and go to the third. Now you can create another URLConnection >> add the Cookie as requestProperty >> and connect, But the problem is: the Cookie(a) is only valid for one session, so when I open the second connection, the Cookie isn't correct anymore. And if you don't send the right Cookie to page3, it'll just return a 'null' for .getHeaderFields.get("Set-Cookie"). So does anyone know how to get that seconds Cookie(b)? -maybe by keeping/restoring/resuming the same session or something..?...I hope this is clear, otherwise just ask me for clarification. Thanks!
You could try using Apache HttpClient, which has cookie handling built into it.
From Java 5 onwards, there is a cookiemanager, that stores all relevant cookies in a map. You do not have to use url.rewrite to manage the cookies. Google for cookiemanager. This is the first link and it is relevant.
精彩评论