出處:http://www.piaoyi.org/c-sharp/378.html
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(getHtml("http://tw.yahoo.com/", null));
}
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(getHtml("http://tw.yahoo.com/", null));
}
//獲取網頁編碼,自動區分網頁編碼
private string getHtml(string url, string charSet)
//url是要訪問的網站地址,charSet是目標網頁的編碼
//如果傳入的是null或者"",就自動分析網頁的編碼
{
WebClient myWebClient = new WebClient();
myWebClient.Proxy = null;
byte[] myDataBuffer = myWebClient.DownloadData(url);
string strWebData = Encoding.Default.GetString(myDataBuffer);
//獲取網頁編碼描述訊息
Match charSetMatch = Regex.Match(strWebData, " ]*)charset=(\")?(.*)?\"", RegexOptions.IgnoreCase | RegexOptions.Multiline);
string webCharSet = charSetMatch.Groups[3].Value;
if (charSet == null || charSet == "") charSet = webCharSet;
if (charSet != null && charSet != "" && Encoding.GetEncoding(charSet) != Encoding.Default)
{
strWebData = Encoding.GetEncoding(charSet).GetString(myDataBuffer);
}
return strWebData;
}
private string getHtml(string url, string charSet)
//url是要訪問的網站地址,charSet是目標網頁的編碼
//如果傳入的是null或者"",就自動分析網頁的編碼
{
WebClient myWebClient = new WebClient();
myWebClient.Proxy = null;
byte[] myDataBuffer = myWebClient.DownloadData(url);
string strWebData = Encoding.Default.GetString(myDataBuffer);
//獲取網頁編碼描述訊息
Match charSetMatch = Regex.Match(strWebData, " ]*)charset=(\")?(.*)?\"", RegexOptions.IgnoreCase | RegexOptions.Multiline);
string webCharSet = charSetMatch.Groups[3].Value;
if (charSet == null || charSet == "") charSet = webCharSet;
if (charSet != null && charSet != "" && Encoding.GetEncoding(charSet) != Encoding.Default)
{
strWebData = Encoding.GetEncoding(charSet).GetString(myDataBuffer);
}
return strWebData;
}
沒有留言:
張貼留言