|
- import java.io.*;
- import java.net.*;
- public class URLClient{
- protected URLConnection connection;
-
- public static void main(String[] args){
- URLClient client = new URLClient();
- String yahoo = client.getDocumentAt("http://www.yahoo.com");
- System.out.println(yahoo);
- }
- public String getDocumentAt(String urlString){
- StringBuffer document = new StringBuffer();
- try{
- URL url = new URL(urlString);
- URLConnection conn = url.openConnection();
- BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
-
- String line = null;
- while((line = reader.readLine()) != null)
- document.append(line + "\n");
- reader.close();
- }catch(MalformedURLException e){
- System.out.println("Unable to connect to URL:" + urlString);
- }catch(IOException e){
- System.out.println("IOException when connecting to URL:" + urlString);
- }
- return document.toString();
- }
- }
复制代码 |
|