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
          主站蜘蛛池模板: 澳门| 友谊县| 普安县| 陕西省| 色达县| 黄骅市| 岳普湖县| 宣汉县| 南和县| 洪湖市| 晋宁县| 永清县| 桐乡市| 衢州市| 通榆县| 都匀市| 增城市| 怀化市| 五常市| 怀安县| 乐东| 攀枝花市| 乐至县| 固阳县| 佛坪县| 聊城市| 诸暨市| 泸定县| 海兴县| 古丈县| 永济市| 桓台县| 永和县| 漯河市| 兴义市| 白银市| 杭锦旗| 乐亭县| 偃师市| 枣阳市| 上饶市|