获取一个url的内容.

上一篇 / 下一篇  2007-03-30 23:49:36 / 个人分类:http

WebPageReader.java

package sdkongkong;
   import java.net.URL;
   import java.net.MalformedURLException;
   import java.net.URLConnection;
   import java.io.IOException;
   import java.io.BufferedReader;
   import java.io.InputStreamReader;

   public class WebPageReader {

      private static URLConnection connection;

      private static void connect( String urlString ) {
        try {
          URL url = new URL(urlString);
          connection = url.openConnection();
          System.out.println(connection.getClass());
        } catch (MalformedURLException e){
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }

      private static void readContents() {
        BufferedReader in = null;
        try {
          in = new BufferedReader(
            new InputStreamReader(
              connection.getInputStream()));

          String inputLine;
          while (
            (inputLine = in.readLine()) != null) {
            System.out.println(inputLine);
          }
        } catch (IOException e) {
          e.printStackTrace();
        }
      }

      public static void main(String[] args) {
        if (args.length != 1) {
          System.err.println("usage: java WebPageReader " + "<url>");
          System.exit(0);
        }
        connect(args[0]);
        readContents();
      }
   }

 

使用方法,java WebPageReader + url(http://www.sina.com)


TAG: http

 

评分:0

我来说两句

我的栏目

日历

« 2024-04-27  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 1559
  • 日志数: 4
  • 建立时间: 2007-03-12
  • 更新时间: 2008-12-31

RSS订阅

Open Toolbar