[an error occurred while processing this directive]
Domain for sale!
Start Search Contents Index Links About
GetResponse.java
/*
 * Author: Havard Rast Blok
 * E-mail:  
 * Web   : www.rememberjava.com
 */

import java.io.*;
import java.net.*;

/**
 * Checks to see if the URL can be found on the server, or if it
 * returns a 404 file not found message.
 
 * Refer to the main() method for how to read a local file instead.
 */
public class GetResponse  
{
  private static String startTag = "<title>";
  private static String endTag = "</title>";
  private static String msg404 = "404";
  private static int startTagLength = startTag.length();

  public GetResponseURL theURL )
  {
    BufferedReader bufReader;
    String line;
    boolean foundStartTag = false;
    boolean foundEndTag = false;
    boolean found404 = false;
    int startIndex, endIndex;
    String title = "";
    
    try
    {
      //open file
      bufReader = new BufferedReadernew InputStreamReader(theURL.openStream()) );
      
      //read line by line
      while( (line = bufReader.readLine()) != null && !foundEndTag && !found404)
      {
        //System.out.println(line);

  //search for line containing 404
  ifline.toLowerCase().indexOf(msg404!= -)
        {
    System.out.println"Found 404 line. This URL was not found on the server" );
          found404 = true;
        }

      
        //search for title start tag (convert to lower case before searhing)
        if!foundStartTag && (startIndex = line.toLowerCase().indexOf(startTag)) != -)
        {
          foundStartTag = true;
        }
        else
        {
          //else copy from start of string
          startIndex = -startTagLength;
        }
        
        //search for title end tag (convert to lower case before searhing)
        iffoundStartTag && (endIndex = line.toLowerCase().indexOf(endTag)) != -)
        {
          foundEndTag = true;
        }
        else
        {
          //else copy to end of string
          endIndex = line.length();
        }
        
        //extract title field
        iffoundStartTag || foundEndTag )
        {
          title += line.substringstartIndex + startTagLength, endIndex );
        }
      }
      
      //close the file when finished
      bufReader.close();
      
      //output the title
      iftitle.length() )
      {
        System.out.println("Title: "+title);
      }
      else
      {
        System.out.println("No title found in document.");
      }
      
    }
    catchIOException e )
    {
      System.out.println"GetResponse.GetResponse - error opening or reading URL: " + e );
    }
  }
  
  
  public static void mainString args[] )
  {
    try
    {
      new GetResponse(new URL(args[0]));
      
      //Open file from disk instead
      //File f = new File( args[0] );
      //new GetResponse( f.toURL() );
    }
    catch (MalformedURLException e)
    {
      System.out.println("GetResponse.main - wrong url: " +e );
    }
    
  }
}


site: Håvard Rast Blok
mail:
updated: 16 July 2010