• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ben,

Are you getting the doc if it's a 500 based error?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 111
Java Linux PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic