Doc Holiday
Doc Holiday

Reputation: 10254

Java HTTP connection.getInputStream() get response from exception

Hello all in my doe I have a try catch and I am catching the exception from a webservice

However If I run my web service in Firefox Poster add-on I get a response as well as a stastus exception

This obviously is not ALL the code but basically the exception is happening at getInputStream()

How can I get the response from the exception?

try{
//Get Response
stream = connection.getInputStream();

} catch (Exception e) {
        throw new CustomException("Exception - " + e.getMessage());

Upvotes: 0

Views: 3436

Answers (1)

erickson
erickson

Reputation: 269677

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int code = connection.getResponseCode();
String msg = connection.getResponseMessage();

These methods will still throw IOException if you can't reach the server. But if the server responds, even with an error, these methods give you access to the response.

Upvotes: 2

Related Questions