• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 111
PHP Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:



 
reply
    Bookmark Topic Watch Topic
  • New Topic