HttpClient及数据输出格式

HttpClient and data output format

我正在使用包含 API.

的第 3 方网络应用程序

我可以连接到 API,并通过 HttpClient 获取数据,但数据格式不正确。

当我直接用浏览器点击 API 时,我得到了这样不错的数据:

 {
  "odata.metadata":"http://ms.ischool.zzz/SOM_Rsite/Api/v1/$metadata#key","value":[
    {
      "odata.id":"http://ms.ischool.zzz/SOM_Rsite/Api/v1/key('123')","Id":"123","Name":"VideoSearch","TimeoutInMinutes":20160,"IsDefault":false,"CreateAuthTicketsForResources":false,"ReportAuthFailureAsError":false,"ExcludePrivatePresentations":true,"Internal":true,"ViewOnlyAccessContext":true
    },{
      "odata.id":"http://ms.ischool.zzz/SOM_Rsite/Api/v1/key('456')","Id":"456","Name":"DesktopRecorder","TimeoutInMinutes":20160,"IsDefault":false,"CreateAuthTicketsForResources":false,"ReportAuthFailureAsError":false,"ExcludePrivatePresentations":true,"Internal":true,"ViewOnlyAccessContext":false
    },{
      "odata.id":"http://ms.ischool.zzz/SOM_Rsite/Api/v1/key('789')","Id":"789","Name":"Manage","TimeoutInMinutes":20160,"IsDefault":false,"CreateAuthTicketsForResources":false,"ReportAuthFailureAsError":false,"ExcludePrivatePresentations":true,"Internal":true,"ViewOnlyAccessContext":false
    }

但是当我在我的代码中使用我的 HttpClient 时,我得到这样的数据:

 ["{\r\n  \"odata.metadata\":\"http://ms.ischool.zzz/SOM_Rsite/Api/v1/$metadata#key\",\"value\":[\r\n    {\r\n      \"odata.id\":\"http://ms.ischool.zzz/SOM_Rsite/Api/v1/key('123')\",\"Id\":\"123\",\"Name\":\"VideoSearch\",\"TimeoutInMinutes\":20160,\"IsDefault\":false,\"CreateAuthTicketsForResources\":false,\"ReportAuthFailureAsError\":false,\"ExcludePrivatePresentations\":true,\"Internal\":true,\"ViewOnlyAccessContext\":true\r\n    },{\r\n      \"odata.id\":\"http://ms.ischool.zzz/SOM_Rsite/Api/v1/key('456')\",\"Id\":\"456\",\"Name\":\"DesktopRecorder\",\"TimeoutInMinutes\":20160,\"IsDefault\":false,\"CreateAuthTicketsForResources\":false,\"ReportAuthFailureAsError\":false,\"ExcludePrivatePresentations\":true,\"Internal\":true,\"ViewOnlyAccessContext\":false\r\n    },{\r\n      \"odata.id\":\"http://ms.ischool.zzz/SOM_Rsite/Api/v1/key('789')\",\"Id\":\"789\",\"Name\":\"Manage\",\"TimeoutInMinutes\":20160,\"IsDefault\":false,\"CreateAuthTicketsForResources\":false,\"ReportAuthFailureAsError\":false,\"ExcludePrivatePresentations\":true,\"Internal\":true,\"ViewOnlyAccessContext\":false\r\n    }"]

我的 HttpClient 代码非常简单。我删除了添加 "application/json" header 的行,但这对数据格式没有影响。

有没有人看出明显的错误?

谢谢!

private const string URL = "http://ms.ischool.zzz/SOM_Rsite/Api/v1";

private async Task<string> GetExternalResponse()
    {
        var byteArray = Encoding.ASCII.GetBytes("username:password");
        HttpClient client = new HttpClient();

        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));

        HttpResponseMessage response = await client.GetAsync(URL);
        string responseBody = await response.Content.ReadAsStringAsync();

        return responseBody;
    }



    public async Task<IEnumerable<string>> Get()
    {
        var result = await GetExternalResponse();

        return new string[] {result};
    }

没有错,一切都很好。您看到的是调试器的 Watch window 中的字符串表示形式。如果您单击它旁边的小放大图标,您会得到一个 window 文本,就像打印出来的一样。

基本上,在程序中声明字符串时,需要对字符串中的某些字符进行转义。双引号就是一个例子。调试器正在显示该字符串的转义视图,即 Watch window 显示字符串的方式与您需要键入它的方式相同,如果您想声明一个具有该值的变量。