Lerul Ler Lerul Ler - 7 months ago 16
Java Question

Response from UrlConnection Java

So far, I have this snippet.

URLConnection connection = null;
try {
connection = (new URL("some_link")).openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.connect();
} 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

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

Comments