WebClient.DownloadString result is not match with Browser result 2

只愿长相守 提交于 2019-12-02 04:11:53

In Linqpad you can run the below code, variation from Webclient. As you can see from the picture, its due to the Gzip compression which browser automatically handles.

async void Main()
{
    using (var handler = new HttpClientHandler())
    {
        handler.AutomaticDecompression = DecompressionMethods.GZip;
        using (HttpClient client = new HttpClient(handler))
        {
            var result = await client.GetStringAsync("http://www.tsetmc.com/tsev2/data/instinfodata.aspx?i=59266699437480384&c=64");
            result.Dump();
        }
    }
}
Nodiel Clavijo Llera

That's not an encoding problem, i think it relates with compression, gzip in this case. Read Uncompressing gzip response from WebClient. That should fix your problem.

public class WC : WebClient
{
    protected override WebRequest GetWebRequest(Uri url)
    {
        var request = base.GetWebRequest(url) as HttpWebRequest;
        request.AutomaticDecompression = DecompressionMethods.GZip;

        return request;
    }
}

Usage:

var url = "http://www.tsetmc.com/tsev2/data/instinfodata.aspx?i=59266699437480384&c=64";
var wc = new WC();
wc.Encoding = Encoding.UTF8;
var result = wc.DownloadString(url);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!