WebClient.DownloadString result is not match with Browser result 2

*爱你&永不变心* 提交于 2019-12-20 04:56:21

问题


The following code:

WebClient wc = new WebClient();
wc.Encoding = Encoding.UTF8;
string Url = "http://www.tsetmc.com/tsev2/data/instinfodata.aspx?i=59266699437480384&c=64";
return wc.DownloadString(Url);

code returns:

�Q�T�MP�J�A|�^D����~���C�"�����l� ��;I&3=j=�iG�H9Ȓ�J�^� �j��T�Q=HH�'Qm�������1�hF�4�*�������{�x�\o?

when I visit that URL in any web browser, I get:

12:29:45,A ,3540,3567,3600,3621,3690,3515,140,238204,849582597,1,20140914,122945;;1@2825@3523@3583@1700@1,1@2000@3522@3600@8700@2,1@500@3511@3640@2500@1,;19774,99736,1

is there any way to get right string?

also, I use this online Decoder, but I don't get right answer: Universal Online Decoder


回答1:


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();
        }
    }
}



回答2:


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.




回答3:


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);


来源:https://stackoverflow.com/questions/34405560/webclient-downloadstring-result-is-not-match-with-browser-result-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!