簡體   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