Skip to content Skip to sidebar Skip to footer

How To Quickly Check If Url Server Is Available

I have a URL in the form http://www.mywebsite.com/util/conv?a=1&from=%s&to=%s And want to check if it is available. The links redirect me on a bad request page if I try to

Solution 1:

staticpublicbooleanisServerReachable(Context context) {
    ConnectivityManagerconnMan= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfonetInfo= connMan.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnected()) {
        try {
            URLurlServer=newURL("your server url");
            HttpURLConnectionurlConn= (HttpURLConnection) urlServer.openConnection();
            urlConn.setConnectTimeout(3000); //<- 3Seconds Timeout 
            urlConn.connect();
            if (urlConn.getResponseCode() == 200) {
                returntrue;
            } else {
                returnfalse;
            }
        } catch (MalformedURLException e1) {
            returnfalse;
        } catch (IOException e) {
            returnfalse;
        }
    }
    returnfalse;
}

or by using runtime:

Runtimeruntime= Runtime.getRuntime();
Processproc= runtime.exec("ping www.serverURL.com"); //<- Try ping -c 1 www.serverURL.comintmPingResult= proc .waitFor();
if(mPingResult == 0){
    returntrue;
}else{
    returnfalse;
}

You can try isReachable() but there is a bug filed for it and this comment says that isReachable() requires root permission:

try {
    InetAddress.getByName("your server url").isReachable(2000); //Replace with your namereturntrue;
} catch (Exception e)
{
    returnfalse;
}

Solution 2:

publicstaticbooleanexists(String URLName) {

        try {
            HttpURLConnection.setFollowRedirects(false);
            // note : you may also need// HttpURLConnection.setInstanceFollowRedirects(false)HttpURLConnection con = (HttpURLConnection) newURL(URLName)
            .openConnection();
            con.setRequestMethod("HEAD");
            return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
        } catch (Exception e) {
            e.printStackTrace();
            returnfalse;
        }
    }

Solution 3:

have you tried using raw sockets?

It should run faster as it's on a lower layer

staticbooleanexists(String serverUrl)  {

    final Socket socket;

    try {
        URLurl=newURL(serverUrl);
        socket = newSocket(url.getHost(), url.getPort());
    } catch (IOException e) {
        returnfalse;
    }

    try {
        socket.close();
    } catch (IOException e) {
        // will never happen, it's thread-safe
    }

    returntrue;
}

Solution 4:

As mentioned by 'eridal', that should be faster, open the socket; however, it only tells you that a server is listening on the host and port but to be sure, you need to write HTTP or alternately a bad request (junk), you should get HTTP/1.1 400 Bad Request. By reading the first line returned, if it contains HTTP, then you are sure that the server is a HTTP server. This way you are sure that the server is available as well as a HTTP server.

This is in extension to the above answer by eridal.

Solution 5:

I had similar problem last month and someone helped me out with an optional example. I'd like to suggest you the same

public boolean isServerReachable()
    // To check if server is reachable
    {
        try {
            InetAddress.getByName("google.com").isReachable(3000); //Replace with your namereturntrue;
        } catch (Exception e) {
            returnfalse;
        }
    }

if return true than your url server is available else is not available currently.

Post a Comment for "How To Quickly Check If Url Server Is Available"