greatjone

          BlogJava 聯(lián)系 聚合 管理
            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
          主站蜘蛛池模板: 北宁市| 澄江县| 山阳县| 虹口区| 东辽县| 南溪县| 七台河市| 遂川县| 新兴县| 赤壁市| 宾阳县| 泌阳县| 达州市| 遂川县| 宁津县| 常山县| 怀仁县| 河北区| 汉源县| 无锡市| 陈巴尔虎旗| 井研县| 嘉荫县| 沙田区| 濮阳市| 阿克| 湘潭县| 关岭| 东至县| 西充县| 沁源县| 石城县| 江门市| 印江| 朝阳区| 泉州市| 宁远县| 通海县| 德江县| 南丹县| 怀化市|