亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

asp.net 網(wǎng)頁編碼自動(dòng)識(shí)別代碼

 更新時(shí)間:2008年09月10日 01:03:15   作者:  
另外一位網(wǎng)友空間/IV提供的代碼,功能同HttpWebRequest獲取網(wǎng)頁源代碼時(shí)自動(dòng)識(shí)別網(wǎng)頁編碼
復(fù)制代碼 代碼如下:

using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

class Program
{
// 獲取網(wǎng)頁的HTML內(nèi)容,根據(jù)網(wǎng)頁的charset自動(dòng)判斷Encoding
static string GetHtml(string url)
{
return GetHtml(url, null);
}

// 獲取網(wǎng)頁的HTML內(nèi)容,指定Encoding
static string GetHtml(string url, Encoding encoding)
{
byte[] buf = new WebClient().DownloadData(url);
if (encoding != null) return encoding.GetString(buf);
string html = Encoding.UTF8.GetString(buf);
encoding = GetEncoding(html);
if (encoding == null || encoding == Encoding.UTF8) return html;
return encoding.GetString(buf);
}

// 根據(jù)網(wǎng)頁的HTML內(nèi)容提取網(wǎng)頁的Encoding
static Encoding GetEncoding(string html)
{
string pattern = @"(?i)\bcharset=(?<charset>[-a-zA-Z_0-9]+)";
string charset = Regex.Match(html, pattern).Groups["charset"].Value;
try { return Encoding.GetEncoding(charset); }
catch (ArgumentException) { return null; }
}

// 程序入口
static void Main()
{
Console.WriteLine(GetHtml(http://chabaoo.cn));

Console.Read();
}
}

相關(guān)文章

最新評論