Meaningless Drivel is fun!*
The moose likes I/O and Streams and the fly likes HttpsURLConnection: Reading Body of Response when response code is not 200 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "HttpsURLConnection: Reading Body of Response when response code is not 200" Watch "HttpsURLConnection: Reading Body of Response when response code is not 200" New topic
Author

HttpsURLConnection: Reading Body of Response when response code is not 200

Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I'm writing a client to connect to some RESTfull web services using HttpsURLConnection.

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?



Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Hi Ben,

Are you getting the doc if it's a 500 based error?


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.

Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Ben Souther wrote:Unfortunately, the call to getOutputStream() throws an IOException when the response code is 400.


I assume you actually mean the call to getInputStream() here.. (that's what your code indicates)
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Got it.
Google is my friend.
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4513568

The answer for anyone else reading this is to call getErrorStream if the response code is not 200.



Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Martijn Verburg wrote:
Ben Souther wrote:Unfortunately, the call to getOutputStream() throws an IOException when the response code is 400.


I assume you actually mean the call to getInputStream() here.. (that's what your code indicates)



Yes, that's what I meant.
Thank you, Martijn.
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

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
Klaas van Gelder
Ranch Hand

Joined: Jul 08, 2004
Posts: 100
Ben Souther wrote:Got it.
Google is my friend.
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4513568

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%)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HttpsURLConnection: Reading Body of Response when response code is not 200
 
Similar Threads
While replacing char of soap response then it throws error
package javax.servlet.annotation does not exist
Problem with using HttpsURLConnection
Some of you are experts this could be your next problem!
how to make URL connection to servlet in j_security_check form based authentication