Next_Line_Nick
Next_Line_Nick

Reputation: 79

testing url connection java html

When I am testing the list of url's to see if the connection is "alive" or "dead", I run into this site: (www.abbapregnancy.org) then site is blank with no information. How can I make an if statement to test for sites like this? Feel free to comment or suggest ideas. My code is currently like this:

try
                    {// Test URL Connection
                    URL url = new URL("http://www."+line);
                    URLConnection conn = url.openConnection();
                    conn.setDoOutput(true);
                    wr = new OutputStreamWriter(conn.getOutputStream());
                    wr.flush();

                    rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

                    while( !rd.ready() ) {}

                    if( rd.ready())
                    {   
                        //write to output file
                        System.out.println("Good URL: " + line);
                        urlAlive ++;
                        //increment alive line to Alive url file
                        aliveUrl += (line + System.getProperty("line.separator"));                                                          
                    }
                    else // increment dead url 
                        urlDead++;
                    }
                    catch (Exception e) 
                    // increment dead url 
                        {
                            urlDead++;
                        }

Upvotes: 1

Views: 5063

Answers (2)

putulputul
putulputul

Reputation: 51

public static boolean IsReachable(Context context, String check_url) {
    // First, check we have any sort of connectivity
    final ConnectivityManager connMgr = (ConnectivityManager)   context.getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo netInfo = connMgr.getActiveNetworkInfo();
    boolean isReachable = false;

    if (netInfo != null && netInfo.isConnected()) {
        // Some sort of connection is open, check if server is reachable
        try {

            URL url = new URL(check_url);

            // URL url = new URL("http://192.168.100.93/office_com/www/api.php/office_com/Logins/SignUp?api_user=endpoint");
            //URL url = new URL("http://10.0.2.2");
            HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
            urlc.setRequestProperty("User-Agent", "Android Application");
            urlc.setRequestProperty("Connection", "close");
            urlc.setConnectTimeout(10 * 1000);
            try {
                urlc.connect();
                System.out.println("-----fffff");
            } catch (Exception e) {

                System.out.println("-----fffff  " + e);

            }
            isReachable = (urlc.getResponseCode() == 200);
        } catch (IOException e) {

        }
    }

    return isReachable;
}

Upvotes: 0

janos
janos

Reputation: 124704

So you want to check more than just "alive" or "dead". If alive, you also want to know if the content is empty, right?

Since you seem to want to check websites, using http connections makes sense here. For the alive/dead condition, connection attempts will throw an exception if dead. If alive, you can use the status code to check if the request was really successful. Finally, you can use the content-length header to check if the site is up but actually returning empty content. For example like this:

public void checkURL(URL url) throws IOException {
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    System.out.println(String.format("Fetching %s ...", url));
    try {
        int responseCode = conn.getResponseCode();
        if (responseCode == 200) {
            System.out.println(String.format("Site is up, content length = %s", conn.getHeaderField("content-length")));
        } else {
            System.out.println(String.format("Site is up, but returns non-ok status = %d", responseCode));
        }
    } catch (java.net.UnknownHostException e) {
        System.out.println("Site is down");
    }
}

Upvotes: 1

Related Questions