John Jiang
John Jiang

Reputation: 11519

Java - Creating a httprequest the uses the header information from previous request

I got some sample code from the net here: http://www.javadb.com/sending-a-post-request-with-parameters-from-a-java-class

That works fine. It sets the relevant headers in the response. However, what if I wanted to visit another url on the same site with those headers? If I create another URL object the session is lost. How do I maintain the session?

Upvotes: 2

Views: 136

Answers (2)

Andrew B
Andrew B

Reputation: 849

You're going at a pretty low level, unless that's intentional, you might want to consider using a library such as Apache Commons HttpClient, which will handle cookie management for you.

Upvotes: 2

harschware
harschware

Reputation: 13424

I don't know the answer to your question offhand but you might want to consider using HttpUnit. It makes a lot of tasks of hitting a server easy (such as authentication, providing form input, etc.). As always there are trade offs. Perhaps it would be too heavy a client for your needs... but it will allow you to maintain a session with the server.

EDIT: On second thought: Commons HttpClient might be better. I believe it is what HttpUnit uses under the hood anyway.

Upvotes: 0

Related Questions