Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

POST gets two answers, only got headers from 2nd... Please help!

843790Feb 7 2009 — edited Feb 21 2009
Hello!

In this attempt to make a software that connects to a webserver (youtube), authenticates and manages a user's address book, I'm using the HttpURLConnection for sending POST data and getting back an answer. Understanding the basics is ok, it works from a Java point of view, but... PLaying with http gets a bit tricky :-)

I've been trying to understand for hours now, and here's the problem: The only HttpURLConnection in use receives a first answer, (with the good data) then a few ms after, it receives a second answer (without that good data).
The headers are read the following way:
Map> headers = http.getHeaderFields();

for (String key : headers.keySet()) {

    for (String value : headers.get(key)) {

        System.out.println(key + ": " + value);

    }

}
(see, I've made nothing new)

The system.out.println only outputs the Set-Cookie headers of the second response. A network packet sniffer shows these two responses:
Frame 9 and Frame 18:

{color:#000000}6 {HTTP:1, TCP:0, IPv4:0} 192.168.1.3 208.117.236.70 HTTP HTTP:Request, POST /login{color}
7 {HTTP:1, TCP:0, IPv4:0} 192.168.1.3 208.117.236.70 HTTP HTTP:HTTP Payload, URL: /login
{color:#3366ff}9 {HTTP:1, TCP:0, IPv4:0} 208.117.236.70 192.168.1.3 HTTP HTTP:Response, HTTP/1.1, Status Code = 303, URL: /login{color}
11 {HTTP:1, TCP:0, IPv4:0} 208.117.236.70 192.168.1.3 HTTP HTTP:HTTP Payload, URL: /login
16 {HTTP:3, TCP:2, IPv4:0} 192.168.1.3 208.117.236.70 HTTP HTTP:Request, GET /index
{color:#3366ff}18 {HTTP:3, TCP:2, IPv4:0} 208.117.236.70 192.168.1.3 HTTP HTTP:Response, HTTP/1.1, Status Code = 200, URL: /index {color}

Frame n°
6 being the post request (headers),
7 the payload of the request (i.e. username, password action_login, etc.),
9 is the the answer containing the needed Set-Cookies ("LOGIN_INFO"),
11 the payload of that answer (contains nothing relevant),
16 hmm not sure about its purpose,
18 is the "second" response to the post request.

Frame 18 does contain a few Set-Cookie headers, but these are useless (i.e. GEO tag, etc. which are different between every request). However, I can't find a way to "prevent" my instance of HttpURLConnection to recover further responses... I tried to put
setRequestProperty("Connection", "close");
instead of "keep-alive", but no, doesn't help :(

Then I thought it was a header's fault in frame 9: "Location" which would impose a redirect and did expect the following code to prevent "getting" or whatever send another response:
setFollowRedirects(false); 
Ok. It's a bit early to call it a happy end... I really need help for this!

Thanks a lot for your time!

Edited by: Neurone-shortage on Feb 7, 2009 4:16 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 21 2009
Added on Feb 7 2009
1 comment
173 views