asp.net 網(wǎng)頁編碼自動識別代碼
更新時間:2008年09月10日 01:03:15 作者:
另外一位網(wǎng)友空間/IV提供的代碼,功能同HttpWebRequest獲取網(wǎng)頁源代碼時自動識別網(wǎng)頁編碼
復制代碼 代碼如下:
using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
class Program
{
// 獲取網(wǎng)頁的HTML內(nèi)容,根據(jù)網(wǎng)頁的charset自動判斷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://www.dbjr.com.cn));
Console.Read();
}
}
您可能感興趣的文章:
- php 判斷網(wǎng)頁是否是utf8編碼的方法
- js 顯示base64編碼的二進制流網(wǎng)頁圖片
- Base64編碼加密JS代碼網(wǎng)頁版
- 多種語言(big5\gbk\gb2312\utf8\Shift_JIS\iso8859-1)的網(wǎng)頁編碼切換解決方案歸納
- ASP+FSO生成的網(wǎng)頁文件默認編碼格式以及轉(zhuǎn)換成UTF-8編碼方法
- ASP UTF-8編碼生成靜態(tài)網(wǎng)頁的函數(shù)
- asp.net HttpWebRequest自動識別網(wǎng)頁編碼
- vbs或asp采集文章時網(wǎng)頁編碼問題
- 網(wǎng)頁語言編碼及asp亂碼問題解決方案
- 判斷網(wǎng)頁編碼的方法python版
相關(guān)文章
asp.net 修飾符介紹(關(guān)于public、private、protected、internal)
這篇文章主要介紹了asp.net 修飾符介紹、關(guān)于public、private、protected、internal,需要的朋友可以參考下2014-08-08將Excel中數(shù)據(jù)導入到Access數(shù)據(jù)庫中的方法
將Excel中數(shù)據(jù)導入到Access數(shù)據(jù)庫中的方法,需要的朋友可以參考一下2013-03-03詳解如何在ASP.NET Core Web API中以三種方式返回數(shù)據(jù)
這篇文章主要介紹了詳解如何在ASP.NET Core Web API中以三種方式返回數(shù)據(jù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01Aspnetpager對GridView分頁并順利導出Excel
這篇文章主要介紹了Aspnetpager對GridView分頁并順利導出Excel的相關(guān)資料,需要的朋友可以參考下2016-04-04如何在ASP.Net Core使用分布式緩存的實現(xiàn)
這篇文章主要介紹了如何在ASP.Net Core使用分布式緩存的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-02-02asp.net利用反射實現(xiàn)給model類賦值的方法
這篇文章主要介紹了asp.net利用反射實現(xiàn)給model類賦值的方法,結(jié)合實例形式分析了asp.net使用反射給model類賦值的操作步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-03-03