webclient中的downloadstring方法下载搜狐的网页(乱码)

本文介绍了一个使用C#进行网页抓取的例子,通过HttpWebRequest获取搜狐汽车新闻页面的内容,并采用GZip压缩流解压数据,最后以GBK编码读取结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HttpWebRequest request = (HttpWebRequest)WebRequest.Create( "http://auto.sohu.com/20110414/n305786728.shtml" );
request.Timeout = 30000;
request.Method =  "GET" ;
request.UserAgent =  "Mozilla/4.0" ;
request.Headers.Add( "Accept-Encoding" "gzip, deflate" );
//设置连接超时时间
Encoding encoding = Encoding.GetEncoding( "GBK" );
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string  result =  "" ;
using (Stream streamReceive = response.GetResponseStream())
{
     using (GZipStream zipStream =  new  GZipStream(streamReceive, CompressionMode.Decompress))
         using  (StreamReader sr =  new  StreamReader(zipStream, encoding))
             result = sr.ReadToEnd();
}
Console.WriteLine(result);
Console.Read();
 
-------------------------------------------------------------------------
//编码方式
            WebClient wc =  new  WebClient();
             //...........
            wc.Encoding = System.Text.Encoding.GetEncoding( "gb2312" );

转载于:https://www.cnblogs.com/wenbinliu/archive/2012/11/29/2794011.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值