/**
*
*/
Package com. duoguo. url;
Import java. io. BufferedReader;
Import java. io. IOException;
Import java. io. InputStreamReader;
Import java.net. MalformedURLException;
Import java.net. URL;
Import java.net. URLConnection;
/**
* @ Author shyboy (chao.shen@duoguo.cn)
*
*/
Public class URLClient {
StringBuilder sb = new StringBuilder ();
/**
* Connect to the server and request the specified document
*
* @ Param urlString
*: Document address
* @ Return: String
*/
Public String getDocumentAt (String urlString ){
Try {
URL url = new URL (urlString); // generate a url object
URLConnection urlConnection = url. openConnection (); // open the url Connection
BufferedReader br = new BufferedReader (new InputStreamReader (
UrlConnection. getInputStream ()));
String line = null;
While (line = br. readLine ())! = Null ){
Sb. append (line + "\ n ");
}
} Catch (MalformedURLException e ){
System. out. println ("cannot connect to URL:" + urlString );
E. printStackTrace ();
} Catch (IOException e ){
System. out. println ("connecting to URL throws exception information:" + urlString );
E. printStackTrace ();
}
Return sb. toString ();
}
/**
* @ Param args
*/
Public static void main (String [] args ){
URLClient client = new URLClient ();
String url = client. getDocumentAt ("http://www.baidu.com ");
System. out. println (url );
}
}
Author: "shenjichao2009"