c#远程取得指定url的网页内容

public static string QueryWebPageContent(string url)
{
try
{
string content = "";
WebClient wc = new WebClient();
byte[] buffer = wc.DownloadData(new Uri(url));

//用默认编码转换网页内容
Encoding encoding = Encoding.Default;
content = Encoding.Default.GetString(buffer);

//得到网页的实际编码
Regex regex = new Regex(@"<meta\s+http-equiv\s*=\s*[""']*Content-Type[""']*\s+content\s*=\s*[""']*text/html;\s*charset=([^""']*)[""']*\s*[/]*>|<meta\s+content\s*=\s*[""']*text/html;\s*charset=([^""']*)[""']*\s+http-equiv\s*=\s*[""']*Content-Type[""']*\s*[/]*>", RegexOptions.IgnoreCase);
Match match = regex.Match(info.Content);

if (match.Groups.Count > 2)
{
try
{
encoding = Encoding.GetEncoding(match.Groups[1].Value + match.Groups[2].Value);
content = encoding.GetString(buffer);
}
catch
{ }
}

return content;
}
catch
{
return "";
}
}

http://zhidao.baidu.com/question/25791162.html

posted @ 2009-03-13 16:31  ljlxyf  阅读(246)  评论(0编辑  收藏  举报