Nemin
Nemin

Reputation: 2037

How to check if URL is blocked by my firewall

I am trying to check if the URL is accessible or not. I am using HttpURLConnection for it. This is now I am implementing it.

public static boolean isUrlAccessible(final String urlToValidate)
            throws WAGException {
        URL url = null;
        HttpURLConnection huc = null;
        int responseCode = -1;
        try {
            url = new URL(urlToValidate);
            huc = (HttpURLConnection) url.openConnection();
            huc.setRequestMethod("HEAD");
            huc.connect();
            responseCode = huc.getResponseCode();
        } catch (final UnknownHostException e) {
            e.printStackTrace();
            System.out.println(e.getMessage()+" "+e.getLocalizedMessage());
            return false;
        } catch (final MalformedURLException e){
            e.printStackTrace();
            System.out.println(e.getMessage()+" "+e.getLocalizedMessage());
            return false;
        } catch (ProtocolException e) {
            e.printStackTrace();
            System.out.println(e.getMessage()+" "+e.getLocalizedMessage());
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(e.getMessage()+" "+e.getLocalizedMessage());
            return false;
        } finally {
            if (huc != null) {
                huc.disconnect();
            }
        }
        return responseCode == 200;
    }

When the Internet is down it throws an UnknownHostException, I wanted to know how do I check if a fire wall is blocking a URL and thats why I get an exception and not because that the URL is not accessible. Also, I am just checking for response code 200 to make sure that the URL is accessible. Are there any other checks I need to perform?

Upvotes: 5

Views: 19019

Answers (3)

Daniel C. Oderbolz
Daniel C. Oderbolz

Reputation: 89

As others have said, the answer is "it depends". Our perimeter firewall for example does a redirect, because we want to show the user a custom screen. In this case, I would try to look into the HTTP Status code (30x).

I think it's hard to write a generic function for something like this, you need to tailor this to your setting or make it very configurable.

Just make sure to remain as generic as possible. If your code for example assumes a redirect to a specific URL, this will beak once the infrastructure changes (which happens more often than anticipated).

Upvotes: 0

user207421
user207421

Reputation: 311022

When the Internet is down it throws an UnknownHostException

No, it throws that when the DNS is down or the host isn't known to DNS.

I wanted to know how do I check if a fire wall is blocking a URL

You will get a connect timeout. In rare cases with obsolete hardware you may get a connection refusal, but I haven't heard of that this century. But you will also get a connect timeout if the host is down.

I am just checking for response code 200 to make sure that the URL is accessible. Are there any other checks I need to perform?

No. But URLs aren't blocked by firewalls. Ports are blocked by firewalls.

Upvotes: 2

RanjithKRaman
RanjithKRaman

Reputation: 46

The exception I have usually seen when a firewall is blocking the connection is "java.net.NoRouteToHostException". Try catching that and see if it helps.

Upvotes: 0

Related Questions