[散分分享代码]HttpWebRequest获取网页源代码时自动识别网页编码

iuhxq 2008-08-21 07:29:16
       /// <summary>
/// 获取源代码
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
static string GetHtml(string url, Encoding encoding)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
StreamReader reader = null;
try
{
request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 20000;
request.AllowAutoRedirect = false;

response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK && response.ContentLength < 1024 * 1024)
{
if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
reader = new StreamReader(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress), encoding);
else
reader = new StreamReader(response.GetResponseStream(), encoding);
string html = reader.ReadToEnd();

return html;
}
}
catch
{
}
finally
{

if (response != null)
{
response.Close();
response = null;
}
if (reader != null)
reader.Close();

if (request != null)
request = null;

}

return string.Empty;
}



GetEncoding方法请查看:GetEncoding
...全文
2175 36 打赏 收藏 转发到动态 举报
写回复
用AI写文章
36 条回复
切换为时间正序
请发表友善的回复…
发表回复
xvv13 2010-06-12
  • 打赏
  • 举报
回复
楼主,我把你代码改了一下
你的代码判断&解析的效率很低,判断下一次网页,解析一次
但是我发现一个问题 却怎么也不明白到底哪里错了!
描述一下:reader1.ReadToEnd 读的都是空字符串

static string GetHtml(string url)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
StreamReader reader = null;
StreamReader reader1 = null;

try
{
request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 20000;
request.AllowAutoRedirect = false;

response = (HttpWebResponse)request.GetResponse();


if (response.StatusCode == HttpStatusCode.OK && response.ContentLength < 1024 * 1024)
{
if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
reader = new StreamReader(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress));
else
reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);

string html = reader.ReadToEnd();

Regex reg_charset = new Regex(@"charset\b\s*=\s*(?<charset>[^""]*)");
if (reg_charset.IsMatch(html))
{
reader1 = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(reg_charset.Match(html).Groups["charset"].Value));
}
else if (response.CharacterSet != string.Empty)
{
reader1=new StreamReader(response.GetResponseStream() ,Encoding.GetEncoding( response.CharacterSet));
}
else
reader1=new StreamReader (response .GetResponseStream() , Encoding.Default);

string src = reader1.ReadToEnd();
return src;
}

}
catch{ }
finally
{
if (response != null)
{
response.Close();
response = null;
}

if (reader != null)
reader.Close();

if (request != null)
request = null;

if (reader1 != null)
reader1 = null;

}
return Encoding.Default.BodyName;

}
superliyubo 2009-06-24
  • 打赏
  • 举报
回复
mark!!!!!!!!!!!!!!!!!!!!!!
Delta 2009-03-20
  • 打赏
  • 举报
回复
收藏了,谢谢分享。
lelepu 2008-09-24
  • 打赏
  • 举报
回复
请教下,你可以识别出www.cg160.cn 网站真正的编码吗
我用的是UFT-8的,但是google说我用的是GBK,我都晕死了 求助大哥了
olanty 2008-09-02
  • 打赏
  • 举报
回复
2楼的那个好啊~
MicroYee 2008-08-21
  • 打赏
  • 举报
回复
关注一下没有编码的处理...呵呵
wuyi8808 2008-08-21
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 iuhxq 的回复:]
你试试读取http://www.51aspx.com站的源代码。
[/Quote]

确实,我的程序会错误地把网页编码识别为UTF-8,从而读出的中文都是乱码。必须这样才是正确的:

GetHtml("http://www.51aspx.com", Encoding.GetEncoding("GB18030"))
Adechen 2008-08-21
  • 打赏
  • 举报
回复
学习,收藏
Adechen 2008-08-21
  • 打赏
  • 举报
回复
学习,收藏
FackMan 2008-08-21
  • 打赏
  • 举报
回复
支持``
感谢楼主分享
FackMan 2008-08-21
  • 打赏
  • 举报
回复
不错~
FackMan 2008-08-21
  • 打赏
  • 举报
回复
不错~
MADfox1983 2008-08-21
  • 打赏
  • 举报
回复
mark学习
一品梅 2008-08-21
  • 打赏
  • 举报
回复
收藏
iuhxq 2008-08-21
  • 打赏
  • 举报
回复
你试试读取http://www.51aspx.com站的源代码。
iuhxq 2008-08-21
  • 打赏
  • 举报
回复
我的小站http://www.svnhost.cn
孟子的站http://dotnet.aspx.cc

都是没有页面编码的。不过刚巧它们都是UTF-8的
iuhxq 2008-08-21
  • 打赏
  • 举报
回复
有的,你看我另外一个站:http://745.cc

这个站需要读取大量的页面,所以会遇到形形色色的网页。
wuyi8808 2008-08-21
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 iuhxq 的回复:]
不过我还有个问题。

编码存在几个地方:
1、页面里
2、http头里

你只从页面里取编码,要是页面里没有的,你的可以分析出编码吗?
[/Quote]

这个倒没有考虑到,不知有没有这种网站的例子,可以试试看。 :)
winflying36 2008-08-21
  • 打赏
  • 举报
回复
学习
greystar 2008-08-21
  • 打赏
  • 举报
回复
不错
加载更多回复(15)

62,073

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧