我有一个简单的代码来获得越南网站的回复: http : //vnexpress.net ,但是有一个小问题。 这是第一次,它下载确定,但在此之后,内容包含这样的未知符号:b \ b \ 0 \ 0 \ 0 \ 0 \ 0 \0 \a`I %&/ m ....问题是什么?
string address = "http://vnexpress.net"; WebClient webClient = new WebClient(); webClient.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11 AlexaToolbar/alxg-3.1"); webClient.Encoding = System.Text.Encoding.UTF8; return webClient.DownloadString(address);I have simple code for getting response from a vietnamese website: http://vnexpress.net , but there is a small problem. For the first time, it downloads ok, but after that, the content contains unknown symbols like this:�\b\0\0\0\0\0\0�\a`I�%&/m.... What is the problem?
string address = "http://vnexpress.net"; WebClient webClient = new WebClient(); webClient.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11 AlexaToolbar/alxg-3.1"); webClient.Encoding = System.Text.Encoding.UTF8; return webClient.DownloadString(address);最满意答案
你会发现响应是GZipped。 除非您创建派生类并修改底层的HttpWebRequest以允许自动解压,否则似乎没有办法使用WebClient下载它。
以下是你如何做到这一点:
public class MyWebClient : WebClient { protected override WebRequest GetWebRequest(Uri address) { var req = base.GetWebRequest(address) as HttpWebRequest; req.AutomaticDecompression = DecompressionMethods.GZip; return req; } }并使用它:
string address = "http://vnexpress.net"; MyWebClient webClient = new MyWebClient(); webClient.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11 AlexaToolbar/alxg-3.1"); webClient.Encoding = System.Text.Encoding.UTF8; return webClient.DownloadString(address);You'll find that the response is GZipped. There doesn't appear to be a way to download that with WebClient, unless you create a derived class and modify the underlying HttpWebRequest to allow automatic decompression.
Here's how you'd do that:
public class MyWebClient : WebClient { protected override WebRequest GetWebRequest(Uri address) { var req = base.GetWebRequest(address) as HttpWebRequest; req.AutomaticDecompression = DecompressionMethods.GZip; return req; } }And to use it:
string address = "http://vnexpress.net"; MyWebClient webClient = new MyWebClient(); webClient.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11 AlexaToolbar/alxg-3.1"); webClient.Encoding = System.Text.Encoding.UTF8; return webClient.DownloadString(address);更多推荐
发布评论