View Single Post
Old Nov 11th, 2007, 9:37 AM   #1
equinox
Newbie
 
Join Date: Nov 2007
Posts: 8
Rep Power: 0 equinox is on a distinguished road
Network Programming Help.

Hi all.

I'm a second year soft ware engineering student and we've been given an assignment in network programming. The assignment specification is to take a URL as a command line argument, download it scorce, examine all it's links and finally, print out a summary of how many broken links occurred.

I've only just started this and I have a problem. I'm un-sure as to how I would find out weather a piece of HTML is actually a link or not. Our lecturer gave us a link to this example.

http://www.exampledepot.com/egs/java.../GetLinks.html

Now I've never done network programming before and this seems very hard to understand to me as the writer didn't comment the code or list the packages he used. Could anyone give me a brief explanation of how I would test a page to find links.??

I've been able to figure out how to read in a url and print out it's scorce without any problem and I've included this code below.

Thanks .

//THIS IS JUST A TEST!!!!!!!!


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

public class WebProject
{

	public static void main(String [] args)
	{	
		List links = new ArrayList();	//will be used to store any links


		try
		{
			URL address = new URL(args[0]);  //take in the url to be searched from args
			
			BufferedReader in = new BufferedReader(new InputStreamReader(address.openStream()));    //open up a "link" to the url
			PrintWriter myFile = new PrintWriter("output.HTML");   //create a printwroter to write the contents to a file, output.html
			
			String input = "";

			while((input = in.readLine()) != null) //continue while the is still data to read
			{
				myFile.println(input);  //print out what has just been read
			}

			in.close();  //close the link
			myFile.close();  //close and save output

		}
	
		catch(MalformedURLException e)
		{
			System.out.println("Error!!, the supplied URL doesn't exist. ");  //an error message
			System.out.println(args[0]);
		}
	
		catch(IOException e)
		{
			System.out.println("Error!!, data handeling problem");  //another warning message
		}
	}

}
equinox is offline   Reply With Quote