views:

216

answers:

2

My current problem is very similar to this one.

I have a downloadFile(URL) function that creates a new HttpURLConnection, opens it, reads it, returns the results. When I call this function on the same URL multiple times, the second time around it almost always returns a response code of -1 (But throws no exception!!!).

The top answer in that question is very helpful, but there are a few things I'm trying to understand.

So, if setting http.keepAlive to false solves the problem, it indicates what exactly? That the server is responding in a way that violates the http protocol? Or more likely, my code is violating the protocol in some way? What will the trace tell me? What should I look for?

And what's the deal with this:

You need to read everything from error stream. Otherwise, it's going to confuse next connection and that's the cause of -1.

Does this mean if the response is some type of error (which would be what response code(s)?), the stream HAS to be fully read? Also, every time I am attempting an http request I am basically creating a new connection, and then disconnect()ing it at the end.

However, in my case I'm not getting a 401 or whatever. It's always a 200. But my second connection almost always fails. Does this mean there's some other data I should be reading that I'm not (in a similar manner that the error stream must be fully read)?

Please help shed some light on this? I feel like there's some fundamental http protocol understanding I'm missing.

PS If I were just using the Apache HttpClient, would I not have to deal with all these protocol details? Does it take care of everything for me?

+2  A: 

The support for keep-alive in the default HTTP URL handler is very buggy. We always turn it off.

Use Apache HttpClient with a pooled connection manager if you want keep-alive. If you don't want change your code, you can get another handler like this one,

http://www.innovation.ch/java/HTTPClient/

If your second connection always fails, that means your server doesn't support keepalive. With Keepalive, the HTTP handler simply leaves connection open (even if you call disconnect). The server closes connection if keep-alive is not supported but the handler doesn't know till you make next request on the connection so the 2nd connection fails.

Regarding the read error stream, it only applies if you get non-200 responses.

ZZ Coder
Thank you. You disable it with this, right? `System.setProperty("http.keepAlive", "false")`. Also, to be robust, would it be worth implementing your code example for ALL non-200 responses?
stormin986
Definitely, you should always read all response. It may contain valuable error messages.
ZZ Coder
A: 

i think you're probably talking about this HttpURLConnection bug, fixed in froyo:

http://code.google.com/p/android/issues/detail?id=2939

see that bug for other workarounds. if this isn't the bug you've hit, please raise a bug with a repeatable test case at http://code.google.com/p/android/issues/entry.

Elliott Hughes