夢幻之旅

          DEBUG - 天道酬勤

             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            671 隨筆 :: 6 文章 :: 256 評論 :: 0 Trackbacks
              今天,終于明白了別人是怎么在網上抓數據的.
          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,并返回它的內容
           * <p>
           * 
           * @author huiwanpeng
           * @time 2008-1-24
           * @see no
           
          */

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

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

              
          public SendPost() {
              }

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

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

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

              
          public void sendPost(String post) {
                  
          /** 打算將URL連接進行輸入 */
                  conn.setDoInput(true);
                  
          /** 打算將URL連接進行輸出 */
                  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() {
                  
          /** 某一行的內容 */
                  String line 
          = null;
                  
          /** 最終內容 */
                  String result 
          = "";
                  try {
                      
          /** 打開到此 URL 引用的資源的通信鏈接 */
                      conn.connect();
                      BufferedReader br 
          = new BufferedReader(new InputStreamReader(conn
                              .getInputStream()));
                      
          /** 一行一行地讀,直到讀完 */
                      
          while ((line = br.readLine()) != null) {
                          result 
          += line + "\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
          主站蜘蛛池模板: 陆丰市| 崇礼县| 固镇县| 三台县| 红安县| 齐齐哈尔市| 铁岭县| 黄陵县| 盘锦市| 甘孜县| 永兴县| 东方市| 安宁市| 隆昌县| 元谋县| 渭源县| 娄烦县| 海晏县| 昌宁县| 梅州市| 砀山县| 高清| 岳池县| 临泉县| 吉林市| 横山县| 临颍县| 景东| 红安县| 青铜峡市| 延津县| 鄱阳县| 黑水县| 襄汾县| 永州市| 南丹县| 墨玉县| 基隆市| 红桥区| 襄垣县| 隆德县|