简体   繁体   English

WebClient编码问题

[英]WebClient Encoding Issue

Good evening, 晚上好,

I'm downloading a .JSON file from a CDN which gives me a 12kb file. 我正在从CDN下载.JSON文件,该文件的文件大小为12kb。 I've opened this file (export.json) and verified that all looks correct from an encoding standpoint and is standard JSON syntax. 我已经打开了此文件(export.json),并从编码的角度验证了所有文件看起来都是正确的,并且它是标准的JSON语法。

I use the following code to download it to my server so I can parse it: 我使用以下代码将其下载到服务器上,以便对其进行解析:

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

I've also tried using the DownloadFile method. 我也尝试过使用DownloadFile方法。 Each time, I get something like this in the .JSON file I generate " b X export.json.tmp \\ R H ~ : )u b @" 每次,我都会在.JSON文件中得到类似的信息,并生成“.b Xexport.json.tmp ... \\ R.H.〜。 : )u b @”

Any ideas what I'm missing? 有什么想法我想念的吗? Thanks. 谢谢。

使用httpclient,httpclient自动分析响应标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM