asp.net 網(wǎng)頁(yè)編碼自動(dòng)識(shí)別代碼
更新時(shí)間:2008年09月10日 01:03:15 作者:
另外一位網(wǎng)友空間/IV提供的代碼,功能同HttpWebRequest獲取網(wǎng)頁(yè)源代碼時(shí)自動(dòng)識(shí)別網(wǎng)頁(yè)編碼
復(fù)制代碼 代碼如下:
using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
class Program
{
// 獲取網(wǎng)頁(yè)的HTML內(nèi)容,根據(jù)網(wǎng)頁(yè)的charset自動(dòng)判斷Encoding
static string GetHtml(string url)
{
return GetHtml(url, null);
}
// 獲取網(wǎng)頁(yè)的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)頁(yè)的HTML內(nèi)容提取網(wǎng)頁(yè)的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://www.dhdzp.com));
Console.Read();
}
}
您可能感興趣的文章:
- php 判斷網(wǎng)頁(yè)是否是utf8編碼的方法
- js 顯示base64編碼的二進(jìn)制流網(wǎng)頁(yè)圖片
- Base64編碼加密JS代碼網(wǎng)頁(yè)版
- 多種語(yǔ)言(big5\gbk\gb2312\utf8\Shift_JIS\iso8859-1)的網(wǎng)頁(yè)編碼切換解決方案歸納
- ASP+FSO生成的網(wǎng)頁(yè)文件默認(rèn)編碼格式以及轉(zhuǎn)換成UTF-8編碼方法
- ASP UTF-8編碼生成靜態(tài)網(wǎng)頁(yè)的函數(shù)
- asp.net HttpWebRequest自動(dòng)識(shí)別網(wǎng)頁(yè)編碼
- vbs或asp采集文章時(shí)網(wǎng)頁(yè)編碼問(wèn)題
- 網(wǎng)頁(yè)語(yǔ)言編碼及asp亂碼問(wèn)題解決方案
- 判斷網(wǎng)頁(yè)編碼的方法python版
相關(guān)文章
比較簡(jiǎn)單的將數(shù)據(jù)信息導(dǎo)入wrod文檔方案(C# for word)
史上最簡(jiǎn)單將數(shù)據(jù)信息導(dǎo)入wrod文檔方案(C# for word)2010-01-01
asp.net實(shí)現(xiàn)的MD5加密和DES加解密算法類完整示例
這篇文章主要介紹了asp.net實(shí)現(xiàn)的MD5加密和DES加解密算法類,結(jié)合完整實(shí)例形式分析了asp.net實(shí)現(xiàn)MD5加密算法及DES加密和解密的相關(guān)技巧,需要的朋友可以參考下2016-07-07
asp.net Javascript獲取CheckBoxList的value
最近在做一個(gè)BS的小項(xiàng)目,記得自己搞asp.net的時(shí)候,還是兩年以前,大部分的東西只是有點(diǎn)印象,忘得差不多了,所以這次也算是溫習(xí)的過(guò)程吧,一邊學(xué)習(xí),一邊趕工,呵呵呵。。。。2009-12-12
ASP.NET MVC 導(dǎo)出Word報(bào)表
本文主要介紹了ASP.NET MVC 導(dǎo)出Word報(bào)表的方法,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02
ASP.NET WebForms實(shí)現(xiàn)全局異常捕獲與處理的最佳實(shí)踐
文章介紹了在ASP.NET WebForms中實(shí)現(xiàn)全局異常捕獲與處理的最佳實(shí)踐,包括在Global.asax中使用Application_Error、在Web.config中配置customErrors、在代碼中使用try-catch、全局異常過(guò)濾以及使用日志記錄庫(kù)等方法,感興趣的朋友一起看看吧2025-01-01

