Lerul Ler Lerul Ler - 2 years ago 131
Java Question

Response from UrlConnection Java

So far, I have this snippet.

URLConnection connection = null;
try {
connection = (new URL("some_link")).openConnection();
} catch (IOException e) {


The possible response codes are 200 and 404 and it's working fine when the response code is 200 (OK). My question is how can I find the response code received by my connection, for example: if the response code is 404, throw an exception and do smth there.

Answer Source

Make it to a type of HTTPUrlConnection. Then you can use .getResponseCode();

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download