march alex's blog
          hello,I am march alex
          posts - 52,comments - 7,trackbacks - 0
          今天用Java實現了一個類URLAnalysis,其中的getContent方法可用于實現獲取特定網頁源代碼,main函數用于測試。
          代碼如下:
          import java.io.BufferedReader;
          import java.io.InputStreamReader;
          import java.net.HttpURLConnection;
          import java.net.URL;

          public class URLAnalysis {
              public static String getContent(String urlString) throws Exception {
                  URL url = new URL(urlString);
                  HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                  BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                  String line;
                  String ans = "";
                  while ((line = reader.readLine()) != null){
                       ans += line + "\n";
                  }
                  return ans;
              }
              public static void main(String[] args) throws Exception {
                  String ans = getContent("http://www.baidu.com");
                  System.out.println(ans);
              }
          }
          posted on 2015-02-11 21:47 marchalex 閱讀(313) 評論(0)  編輯  收藏 所屬分類: java小程序
          主站蜘蛛池模板: 铁岭县| 新津县| 汝州市| 鹿泉市| 林芝县| 阳新县| 云南省| 平度市| 沅陵县| 丹棱县| 罗定市| 辛集市| 临邑县| 枞阳县| 繁昌县| 法库县| 吕梁市| 特克斯县| 桦南县| 普洱| 曲靖市| 沅江市| 芜湖市| 西和县| 江陵县| 集贤县| 阿勒泰市| 青岛市| 新源县| 宜都市| 宁晋县| 揭东县| 宜兴市| 沙洋县| 名山县| 三原县| 谢通门县| 伊金霍洛旗| 合川市| 屏东县| 莎车县|