![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2007
Posts: 8
Rep Power: 0
![]() |
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
}
}
} |
|
|
|
|
|
#2 |
|
Not a user?
Join Date: Sep 2007
Posts: 256
Rep Power: 2
![]() |
Re: Network Programming Help.
I would think a normal ping would work, as in if you ping the url, it either is tied to the server or it's not. But then, I don't know that much about networking.
|
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Re: Network Programming Help.
You could connect to the server and still have a broken link. That is, the server might return a 404 page, for instance.
Your first step is to parse the page for all links. That's the emphasis of the link you posted. The next step is to follow all the links and see if you get a valid page returned (200 OK, for instance).
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#4 |
|
Sexy Programmer
|
Re: Network Programming Help.
HttpURLConnection has a method called getResponseCode() and returns 200 if its a valid page. Once you parse the links from the HTML source, you can put them into a list and iterate through the list of urls to see if they are valid. Here is a method that you could use. My Java skills are a bit rusty so bare with me.
public bool isValidUrl(String urlStr)
{
try
{
java.net.URL url = new java.net.URL(urlStr);
java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection)url.openConnection();
httpConn.connect();
if(httpConn.getResponseCode() != 200)
return false;
else
return true; //it does return 200 and is a valid link
}
catch(Exception e)
{
e.printStackTrace();
}
}
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Nov 2007
Posts: 8
Rep Power: 0
![]() |
Re: Network Programming Help.
Thanks guys, I got that code in the link to work (after a few hours and about 5 pints of cofee !!
). I have all my methods worked out so putting the program together should be a breeze. Thanks . |
|
|
|
|
|
#6 |
|
12 years old
Join Date: Nov 2007
Posts: 80
Rep Power: 1
![]() |
Re: Network Programming Help.
java Syntax (Toggle Plain Text)
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| best network programming language | ShadowAssasin | C++ | 11 | Jun 13th, 2006 11:18 AM |
| Network programming | jayme | C++ | 14 | Dec 4th, 2005 9:15 AM |
| Network programming newb | cyberphreak | C++ | 16 | Oct 24th, 2005 7:47 PM |
| Need examples of Network programming using /dev/tcp and /dev/udp | Ed Toro | Bash / Shell Scripting | 3 | Sep 7th, 2005 4:09 PM |
| New to perl: Network programming question | nick | Perl | 1 | Aug 22nd, 2005 4:44 AM |