HttpURLConnection: What is the minimum best-practice implementation?
Posted
by stormin986
on Stack Overflow
See other posts from Stack Overflow
or by stormin986
Published on 2010-05-08T03:10:34Z
Indexed on
2010/05/08
3:18 UTC
Read the original article
Hit count: 515
java
|httpurlconnection
I've come across a lot of HttpURLConnection examples that are nothing more than openConnection(), getInputStream(), and then they just read the buffer and are done. It's simple but seems like it's not the best implementation ... it handles no problems.
I don't yet know much about http, so I keep thinking I have everything covered until a new problem arises.
I'm currently experiencing a similar problem to this one. Most times I try to read the same resource a second time (from a different HttpURLConnection object, after I .disconnect()'ed the previous one), the response code returns as -1 (but no exception is thrown!!). Before I knew to check the response code, I was baffled since I was throwing no exceptions.
So, is there a minimum 'best practice' HttpURLConnection implementation? What are notable exceptions to handle? Request code checking? Any other error checks? What connection parameters do and don't need to be set (like doInput / doOutput, are these even necessary? Some examples have em, some don't). Etc.
I realize this is kind of a broad question but I think it has potential to be a very useful resource if many of the common use cases and FAQs are addressed in one central place. This seems like the kind of thing a community wiki would be good for...
© Stack Overflow or respective owner