This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I know that these services, when a failure occurs will return an HTTP response code (500, 400, etc) and also send and XML document in the response body that contains an error message. Unfortunately, the call to getOutputStream() throws an IOException when the response code is 400.
Is there another way to read the response that allows me to see both the HTTP response code and the body of the response under these condition?
I haven't seen a 500 error yet.
I'm guessing that I would. Most containers send back some type of error screen when 500 errors occur.
I have seen 400 errors and I know, from writing a client in a different technology, that there is a body containing an XML document with information about why the request failed. I just don't seem to be able to retrieve it using HttpsURLConnection.
Oh apart from that I can't see anything else that's wrong, your code seems to match most examples I can see out there. Perhaps you could switch on/off some of the connection settings (such as setDoInput to false) to see if that makes a subtle difference.
Ah, I see you've solved it already, really interesting actually because I was looking at examples where people were complaining that they were getting an error page as part of their regular inputstream
The answer for anyone else reading this is to call getErrorStream if the response code is not 200.
I thought you needed to call getErrorStream() for all response codes other than 200 OK. In this example, get ErrorSteam will be called for all status codes below 400, including 200.
So in my wrapper for Android I use the test:
"What you don't know, can't help you"
SCJP (81%), SCWCD (81%), SCJD (354/400), SCBCD (85%)