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 閱讀(380) 評論(0)  編輯  收藏 所屬分類: java
          主站蜘蛛池模板: 宁强县| 灵川县| 广宗县| 儋州市| 松桃| 屏南县| 长武县| 莱芜市| 安国市| 文登市| 思南县| 衡水市| 浦城县| 甘泉县| 北票市| 铜梁县| 晋城| 乌兰察布市| 民丰县| 泸水县| 梁河县| 阿拉尔市| 阳谷县| 蕉岭县| 桦南县| 民权县| 江阴市| 黔西| 广安市| 乾安县| 灵台县| 尉犁县| 台州市| 宁城县| 九龙坡区| 紫云| 连城县| 永定县| 兴安县| 绥德县| 凤翔县|