2015-12-21 79 views
1

下面的代碼:WebClient.DownloadString結果不匹配瀏覽器結果2

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

代碼返回:

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

,當我訪問該網址在任何網絡瀏覽器,我得到:

12:29:45,A ,3540,3567,3600,3621,3690,3515,140,238204,849582597,1,20140914,122945;;[email protected]@[email protected]@[email protected],[email protected]@[email protected]@[email protected],[email protected]@[email protected]@[email protected],;19774,99736,1 

有什麼方法可以得到正確的字符串?

也,我用這個網絡解碼器,但我沒有得到正確的答案: Universal Online Decoder

回答

2

在Linqpad可以從Web客戶端運行下面​​的代碼,變化。正如你從圖片中看到的,它是由於瀏覽器自動處理的Gzip壓縮。 enter image description here

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
public class WC : WebClient 
{ 
    protected override WebRequest GetWebRequest(Uri url) 
    { 
     var request = base.GetWebRequest(url) as HttpWebRequest; 
     request.AutomaticDecompression = DecompressionMethods.GZip; 

     return request; 
    } 
} 

用法:

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