Lerul Ler Lerul Ler - 1 year ago 65
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.


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