using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace Web
{
? ?/// <summary>??
? ?/// 公共方法類??
? ?/// </summary>??
? ?public class WebHandler
? ?{
? ? ? /// <summary>??
? ? ? /// 獲取網頁的HTML碼??
? ? ? /// </summary>??
? ? ? /// <param name="url">鏈接地址</param>??
? ? ? /// <param name="encoding">編碼類型</param>??
? ? ? /// <returns></returns>??
? ? ? public static string GetHtmlStr(string url, string encoding)
? ? ? {
? ? ? ? ?string htmlStr = "";
? ? ? ? ?try
? ? ? ? ?{
? ? ? ? ? ? if (!String.IsNullOrEmpty(url))
? ? ? ? ? ? {
? ? ? ? ? ? ? ?WebRequest request = WebRequest.Create(url);? ? ? ? ? ? //實例化WebRequest對象??
? ? ? ? ? ? ? ?WebResponse response = request.GetResponse();? ? ? ? ? ?//創建WebResponse對象??
? ? ? ? ? ? ? ?Stream datastream = response.GetResponseStream();? ? ? ?//創建流對象??
? ? ? ? ? ? ? ?Encoding ec = Encoding.Default;
? ? ? ? ? ? ? ?if (encoding == "UTF8")
? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ec = Encoding.UTF8;
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?else if (encoding == "Default")
? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ec = Encoding.Default;
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?StreamReader reader = new StreamReader(datastream, ec);
? ? ? ? ? ? ? ?htmlStr = reader.ReadToEnd();? ? ? ? ? ? ? ? ? //讀取網頁內容??
? ? ? ? ? ? ? ?reader.Close();
? ? ? ? ? ? ? ?datastream.Close();
? ? ? ? ? ? ? ?response.Close();
? ? ? ? ? ? }
? ? ? ? ?}
? ? ? ? ?catch { }
? ? ? ? ?return htmlStr;
? ? ? }
? ?}??
? ?
}