1

I have a java stand-alone application that exposes two methods through an interface: get - to make GET requests to a URL, and post - to make POST requests to a URL. I am using pure Java for implementing the methods (no Apache HTTP client).

My problem is that on HTTP error returns from the server, I get an IOException client-side (for all codes >= 400). It's little point in arguing whether this is a correct behavior (IMHO it's not). Anyway, I would need to check whether the IOException was caused by a connection problem, or by an HTTP error, as I have to implement different behaviors for these two situations. Does anyone know a decent solution to check this? (by decent I mean not ahving to check for the exception message "Server returned HTTP response code")

Any ideas are really appreciated.

Thanks.

1 Answer 1

3

There's not much you can do other than parse the exception string. If you look in the source of HttpURLConnection:

            if (respCode >= 400) {
                if (respCode == 404 || respCode == 410) {
                    throw new FileNotFoundException(url.toString());
                } else {
                    throw new java.io.IOException("Server returned HTTP" +
                          " response code: " + respCode + " for URL: " +
                          url.toString());
                }
            }

you'll see it's not very helpful.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.