greatjone

          BlogJava 聯系 聚合 管理
            7 Posts :: 24 Stories :: 3 Comments :: 0 Trackbacks
          使用HttpURLConnection來訪問web頁面。
           1import java.io.BufferedReader;
           2import java.io.IOException;
           3import java.io.InputStreamReader;
           4import java.net.MalformedURLException;
           5import java.net.URL;
           6import java.net.URLConnection;
           7
           8
           9public class WebPageReader{
          10    private static URLConnection connection;
          11    private static void connect(String urlString){
          12        try {
          13            URL url = new URL(urlString);
          14            connection = url.openConnection();
          15        }
           catch (MalformedURLException e) {
          16            e.printStackTrace();
          17        }
           catch (IOException e) {
          18            e.printStackTrace();
          19        }

          20    }

          21    private static void readContents(){
          22        BufferedReader in = null;
          23        try {
          24            in= new BufferedReader(new InputStreamReader(connection.getInputStream()));
          25            String inputLine;
          26            while((inputLine=in.readLine())!=null){
          27                System.out.println(inputLine);
          28            }

          29        }
           catch (IOException e){
          30            e.printStackTrace();
          31        }

          32    }

          33    public static void main(String[] args){
          34        connect("http://www.google.com.hk/webhp?client=aff-cs-360se&ie=utf-8&oe=UTF-8");
          35        readContents();
          36    }

          37}
          posted on 2010-06-06 16:22 jone 閱讀(377) 評論(0)  編輯  收藏 所屬分類: java
          主站蜘蛛池模板: 漯河市| 丽江市| 铁力市| 固镇县| 香港| 铜梁县| 民勤县| 凤台县| 白玉县| 元谋县| 丹凤县| 林口县| 金湖县| 呼伦贝尔市| 临邑县| 南投市| 定兴县| 蛟河市| 石景山区| 外汇| 日土县| 万山特区| 全州县| 淳安县| 涿鹿县| 云浮市| 福建省| 馆陶县| 胶州市| 赤水市| 逊克县| 广州市| 塘沽区| 安丘市| 若尔盖县| 疏附县| 巴彦县| 平南县| 江永县| 万州区| 银川市|