夢幻之旅

          DEBUG - 天道酬勤

             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            671 隨筆 :: 6 文章 :: 256 評論 :: 0 Trackbacks
          <2008年1月>
          303112345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          公告

          本博客中未注原創(chuàng)的文章均為轉(zhuǎn)載,對轉(zhuǎn)載內(nèi)容可能做了些修改和增加圖片注釋,如果侵犯了您的版權(quán),或沒有注明原作者,請諒解

          常用鏈接

          留言簿(21)

          隨筆分類(644)

          隨筆檔案(669)

          文章檔案(6)

          最新隨筆

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

              今天,終于明白了別人是怎么在網(wǎng)上抓數(shù)據(jù)的.
          package com.roadway.phserver.test;

          import java.io.BufferedReader;
          import java.io.IOException;
          import java.io.InputStreamReader;
          import java.io.PrintWriter;
          import java.net.MalformedURLException;
          import java.net.URL;
          import java.net.URLConnection;

          /**
           * <p>
           * 本類用于Post一個URL,并返回它的內(nèi)容
           * <p>
           * 
           * @author huiwanpeng
           * @time 2008-1-24
           * @see no
           
          */

          public class SendPost {
              
          /** url */
              private URL url;

              
          /** url連接 */
              private URLConnection conn;

              
          public SendPost() {
              }

              
          /**
               * <p>
               * 本方法根據(jù)一個字符串創(chuàng)建一個URL,并打開URL的連接
               * <p>
               * 
               * @param urlAddr
               *            URL地址
               
          */

              
          public void setURL(String urlAddr) {
                  try {
                      
          /** 創(chuàng)建一個URL */
                      url 
          = new URL(urlAddr);
                      
          /** 打開URL連接 */
                      conn 
          = url.openConnection();
                  } catch (MalformedURLException ex) {
                      
          /** 錯誤URL產(chǎn)生異常 */
                      ex.printStackTrace();
                  } catch (IOException ex) {
                      
          /** 輸入輸出異常 */
                      ex.printStackTrace();
                  }
              }

              
          /**
               * <p>
               * 本方法用于POST一個消息
               * <p>
               * 
               * @param post
               *            要POST的參數(shù),比如user=huiwanpeng&password=hwp##
               
          */

              
          public void sendPost(String post) {
                  
          /** 打算將URL連接進(jìn)行輸入 */
                  conn.setDoInput(true);
                  
          /** 打算將URL連接進(jìn)行輸出 */
                  conn.setDoOutput(true);
                  
          /** 聲明的一個打印輸出流 */
                  PrintWriter pw 
          = null;
                  try {
                      pw 
          = new PrintWriter(conn.getOutputStream());
                      pw.
          print(post);
                  } catch (IOException e) {
                      e.printStackTrace();
                  } finally {
                      pw.
          close();
                  }
              }

              
          public String getContent() {
                  
          /** 某一行的內(nèi)容 */
                  String line 
          = null;
                  
          /** 最終內(nèi)容 */
                  String result 
          = "";
                  try {
                      
          /** 打開到此 URL 引用的資源的通信鏈接 */
                      conn.connect();
                      BufferedReader br 
          = new BufferedReader(new InputStreamReader(conn
                              .getInputStream()));
                      
          /** 一行一行地讀,直到讀完 */
                      
          while ((line = br.readLine()) != null) {
                          result 
          += line + "\n";
                      }
                      
          /** 關(guān)閉連接 */
                      br.
          close();
                  } catch (IOException ex) {
                      ex.printStackTrace();
                  }
                  
          return result;
              }

              
          public static void main(String[] args) {
                  String urlAddr 
          = "http://www.ip138.com:8080/search.asp";
                  String post 
          = "action=mobile&mobile=13501678250";
                  SendPost test 
          = new SendPost();
                  test.setURL(urlAddr);
                  test.sendPost(post);
                  String aa 
          = test.getContent().trim();
                  System.out.println(aa);
              }
          }
          posted on 2008-01-24 16:37 HUIKK 閱讀(376) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 娄底市| 梁平县| 沁阳市| 仁化县| 右玉县| 韩城市| 凤城市| 普格县| 普兰店市| 青河县| 庆阳市| 汽车| 沙河市| 望奎县| 海门市| 营口市| 涿州市| 天柱县| 化州市| 遂川县| 于都县| 壶关县| 塘沽区| 康马县| 沁水县| 茂名市| 元氏县| 昭苏县| 福建省| 临潭县| 通许县| 贡山| 临桂县| 碌曲县| 闸北区| 马鞍山市| 资中县| 措勤县| 微博| 昔阳县| 互助|