Next_Line_Nick Next_Line_Nick - 5 months ago 9x
Java Question

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: ( 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:

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

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
catch (Exception e)
// increment dead url


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();
    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 ( e) {
        System.out.println("Site is down");