日期:2014-05-17 浏览次数:20764 次
HttpWebResponse res;
string charSet = "";
try
{
WebClient myWebClient = new WebClient();//创建WebClient实例myWebClient
myWebClient.Credentials = CredentialCache.DefaultNetworkCredentials;
byte[] myDataBuffer = myWebClient.DownloadData(url);
string strWebData = Encoding.Default.GetString(myDataBuffer);
//获取网页字符编码描述信息
Match charSetMatch = Regex.Match(strWebData, "<meta([^<]*)charset=([^<]*)\"", RegexOptions.IgnoreCase | RegexOptions.Multiline);
string webCharSet = charSetMatch.Groups[2].Value.Replace("\"", "");
if (charSet == null || charSet == "")
charSet = webCharSet;
if (charSet != null && charSet != "" && Encoding.GetEncoding(charSet) != Encoding.Default)
strWebData = Encoding.GetEncoding(charSet).GetString(myDataBuffer);
return strWebData;
}
catch (WebException ex)
{