Skip to main content
gamedev.net
🔒 Locked

[java] Grabbing HTML source off the web

Started by Kevinator 2 replies 1.1k views
Original Post
Kevinator
Kevinator
Hi guys. I want to implement an application in Java that lets the user type in a URL, then it will grab the source code at that URL, process it, and spit out some results. I have a clear vision of how I will proceed once I have the html file, but I don't know how to snag the source code off the internet in the first place. Can anyone point me in even vaguely the right direction?
SiCrane
SiCrane
Provided you already know how to open a TCP socket, then take a look at RFC 2616 (HTTP 1.1)
bleb
bleb
... or you could avoid reinventing the wheel and try this:

 private String fetchHTML( String url ) {  StringBuffer buff = new StringBuffer();   try  {   URL query = new URL( url );   URLConnection connection = query.openConnection();   connection.connect();   BufferedReader br = new BufferedReader( new InputStreamReader( connection.getInputStream() ) );   String line = null;   do   {    line = br.readLine();    buff.append( line );   }   while( line != null );      br.close();  }  catch( Exception e )  {   e.printStackTrace();   return "";  }  return buff.toString(); }


There's probably clean-up problems that might have to be handled in a finally clause, but it should get you started.
Kevinator
Kevinator
WOW! Thanks, bleb! I figured there must be something somewhere in Java's gigantic library to handle something like this, I just didn't know where to look. [grin]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.