InPowerS.Net

 找回密碼
 註冊
搜索
查看: 1870|回復: 0

Web application on the browser 網頁內容攔截程式

[複製鏈接]
發表於 2009-12-12 16:20:15 | 顯示全部樓層 |閱讀模式
本帖最後由 AlfaChiu 於 2009-12-12 16:21 編輯

有朋友問我關於網頁攔截程式,這邊貼上函式用法給大家參考。

  1. using System;
  2. using System.Collections;
  3. using System.Net;
  4. using System.IO;
  5. using System.Text;
  6. namespace webpick
  7. {
  8. /// <summary>
  9. /// com 公用函式
  10. /// author: AlfaChiu
  11. /// date: 2009/05/17
  12. /// </summary>
  13. public static class com
  14. {
  15.   /// <summary>
  16.   /// 暫存陣列大小
  17.   /// </summary>
  18.   const int buffsize = 40960;
  19.   /// <summary>
  20.   /// 攔截網頁內容程式 - 分段網址
  21.   /// </summary>
  22.   /// <param name="addr_prifix">網址前導字串</param>
  23.   /// <param name="addr_middle">網址中間字串</param>
  24.   /// <param name="addr_subfix">網址末尾字串</param>
  25.   /// <returns>網頁內容字串</returns>
  26.   /// 範例:
  27.   /// string address_pri = "http://wowbox.yatta.com.tw/item-";
  28.   /// string address_mid = "39238";
  29.   /// string address_sub = ".html";
  30.   /// string page_cont = com.browsing(address_pri, address_mid, address_sub);
  31.   public static string browsing(string addr_prifix, string addr_middle, string addr_subfix)
  32.   {
  33.    HttpWebRequest  myReq = (HttpWebRequest)WebRequest.Create(addr_prifix+addr_middle+addr_subfix);
  34.    HttpWebResponse myRes = (HttpWebResponse)myReq.GetResponse();
  35.    Stream myStr = myRes.GetResponseStream();
  36.    StreamReader streamRead = new StreamReader( myStr );
  37.    string myResponse = "";
  38.    char[] readBuff = new char[com.buffsize];
  39.    int count = streamRead.Read(readBuff, 0, com.buffsize);
  40.    while (count > 0)
  41.    {
  42.     string outputData = new string(readBuff, 0, count);
  43.     myResponse += outputData;
  44.     count = streamRead.Read(readBuff, 0, com.buffsize);
  45.    }
  46.    return myResponse;
  47.   }
  48.   /// <summary>
  49.   /// 攔截網頁內容程式 - 單一網址
  50.   /// </summary>
  51.   /// <param name="addr">網頁網址</param>
  52.   /// <returns>網頁內容字串</returns>
  53.   /// 範例:
  54.   /// string address = "http://wowbox.yatta.com.tw/item-39238.html";
  55.   /// string page_cont = com.browsing(address);
  56.   public static string browsing(string address)
  57.   {
  58.    return browsing(address, "", "");
  59.   }
  60. }
複製代碼
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

小黑屋|Archiver|手機版|InPowerS.Net

GMT+8, 2025-4-11 22:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回復 返回頂部 返回列表