2017-03-06 11 views
0

おはよう、WebClientエンコーディングの問題

私は12kbのファイルを提供するCDNから.JSONファイルをダウンロードしています。私はこのファイル(export.json)を開いて、エンコーディングの観点から見て、すべて標準のJSON構文であることを確認しました。

私はそれを解析することができますので、私のサーバーにそれをダウンロードするには、次のコードを使用します。

//webclient downloads the JSON file 
    var webClient = new WebClient(); 
    webClient.Encoding = System.Text.Encoding.UTF8; 
    string result = string.Empty; 
    try 
    { 
     result = webClient.DownloadString("somewebsite/export.json"); 
     File.WriteAllText(@"C:\Users\WebDev\Documents\Visual Studio 2015\WebSites\test\json.json", result); 
    } 
    catch (Exception ex) { 
     System.Diagnostics.Debug.Write(ex); 
    } 

     //parses downloaded file  

     Newtonsoft.Json.Linq.JObject o = Newtonsoft.Json.Linq.JObject.Parse(File.ReadAllText(@"C:\Users\WebDev\Documents\Visual Studio 2015\WebSites\test\json.json")); 
     //creates a dictionary at the outer object level (key=id and value=inner object 
     var dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(o.ToString()); 

私もDownloadFileメソッドを使用して試してみました。たびに、私は " b Xexport.json.tmp ¥ R H 〜 : )u b @"を生成する.JSONファイルでこれを取得します。 "

アイデアは何ですか?ありがとう。

+0

応答ヘッダーからエンコードを取得する[このソリューション](http://stackoverflow.com/a/30049848/3744182)を試してください。 – dbc

答えて

0

httpclient、httpclient自動分析応答ヘッダーを使用します。