繁体   English   中英

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

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

我使用以下代码获取页面源,但它不返回属性数据:

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

网站的内容编码是gzip

图片

通过设置client.Headers["Accept-Encoding"] = "gzip"; 您要求服务器发送压缩响应。 但是,您没有解压缩它。 这导致了不正确的响应。

根据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