简体   繁体   English

C#中使用Webclient获取页面资源

[英]Use Webclient to get page resource in C#

I use below code for get page Source, but it doesn't return property data :我使用以下代码获取页面源,但它不返回属性数据:

string url = "http://www.tsetmc.com/Loader.aspx?ParTree=15131F";
            WebClient client = new WebClient();
            client.Headers["Accept-Encoding"] = "gzip";
            string pageSource = client.DownloadString(url);

The Content-Encoding of website is gzip网站的内容编码是gzip

图片

By setting client.Headers["Accept-Encoding"] = "gzip";通过设置client.Headers["Accept-Encoding"] = "gzip"; you are asking the server to send a compressed response.您要求服务器发送压缩响应。 However, you are not decompressing it.但是,您没有解压缩它。 This is causing the incorrect response.这导致了不正确的响应。

As per https://stackoverflow.com/a/4914874/23633 , you can get WebClient to automatically decompress responses by modifying the HttpWebRequest it creates:根据https://stackoverflow.com/a/4914874/23633 ,您可以通过修改它创建的HttpWebRequest来让WebClient自动解压缩响应:

class MyWebClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        var request = (HttpWebRequest) base.GetWebRequest(address);
        request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
        return request;
    }
}

string url = "http://www.tsetmc.com/Loader.aspx?ParTree=15131F";
WebClient client = new MyWebClient();
// don't set the Accept-Encoding header here; it will be done automatically
string pageSource = client.DownloadString(url);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM