AlfaChiu 發表於 2009-12-12 16:20:15

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

本帖最後由 AlfaChiu 於 2009-12-12 16:21 編輯

有朋友問我關於網頁攔截程式,這邊貼上函式用法給大家參考。
using System;
using System.Collections;
using System.Net;
using System.IO;
using System.Text;
namespace webpick
{
/// <summary>
/// com 公用函式
/// author: AlfaChiu
/// date: 2009/05/17
/// </summary>
public static class com
{
/// <summary>
/// 暫存陣列大小
/// </summary>
const int buffsize = 40960;
/// <summary>
/// 攔截網頁內容程式 - 分段網址
/// </summary>
/// <param name="addr_prifix">網址前導字串</param>
/// <param name="addr_middle">網址中間字串</param>
/// <param name="addr_subfix">網址末尾字串</param>
/// <returns>網頁內容字串</returns>
/// 範例:
/// string address_pri = "http://wowbox.yatta.com.tw/item-";
/// string address_mid = "39238";
/// string address_sub = ".html";
/// string page_cont = com.browsing(address_pri, address_mid, address_sub);
public static string browsing(string addr_prifix, string addr_middle, string addr_subfix)
{
   HttpWebRequestmyReq = (HttpWebRequest)WebRequest.Create(addr_prifix+addr_middle+addr_subfix);
   HttpWebResponse myRes = (HttpWebResponse)myReq.GetResponse();
   Stream myStr = myRes.GetResponseStream();
   StreamReader streamRead = new StreamReader( myStr );
   string myResponse = "";
   char[] readBuff = new char;
   int count = streamRead.Read(readBuff, 0, com.buffsize);
   while (count > 0)
   {
    string outputData = new string(readBuff, 0, count);
    myResponse += outputData;
    count = streamRead.Read(readBuff, 0, com.buffsize);
   }
   return myResponse;
}
/// <summary>
/// 攔截網頁內容程式 - 單一網址
/// </summary>
/// <param name="addr">網頁網址</param>
/// <returns>網頁內容字串</returns>
/// 範例:
/// string address = "http://wowbox.yatta.com.tw/item-39238.html";
/// string page_cont = com.browsing(address);
public static string browsing(string address)
{
   return browsing(address, "", "");
}
}
頁: [1]
查看完整版本: Web application on the browser 網頁內容攔截程式