My search results indicate that a lot of people have had more or less the same problem with server responses from java based clients when using the HttpURLConnection class.
First of all, some problems may be caused by the fact that the method setDoOutput(true) changes the request type from GET to POST. Unless you want to do a POST to the server you should not setDoOutput to true. This may cause a 405 bad method response from the server if POST requests are not allowed.
In my case I had a resource accepting both GET and POST requests. It worked fine from the browser. GET worked from the java based client but POST just returned the 405 status.
Finally the problem turned out to be that I was posting to a nice URL using a default document. Explicitly posting to the document (in this case an aspx-file) solved the problem.