2016-07-21 98 views
3

我已經創建了一個控制器和方法,用於從API獲取數據。 我使用HttpResponseMessage來獲得響應。一切工作正常,但我無法得到我想要的JSON格式的確切數據。
方法看起來象下面這樣:C# - 從HttpResponseMessage獲取API值

public class TestController : ApiController 
    { 
     [HttpGet] 

     public async Task<IHttpActionResult> TestMethod(...) 
     { 
      string _apiUrl = String.Format("..."); 
      string _baseAddress = "..."; 

      using (var client = new HttpClient()) 
      { 
       client.BaseAddress = new Uri(_baseAddress); 
       client.DefaultRequestHeaders.Accept.Clear(); 
       client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
       HttpResponseMessage response = await client.GetAsync(_apiUrl); 


       if (response.IsSuccessStatusCode) 
       { 
        return Json(response); 
       } 
      } 
      return NotFound(); 
     } 
    } 

我得到的迴應看起來像:

{ 
    "Version": { 
    "Major": 1, 
    "Minor": 1, 
    "Build": -1, 
    "Revision": -1, 
    "MajorRevision": -1, 
    "MinorRevision": -1 
    }, 
    "Content": { 
    "Headers": [ 
     { 
     "Key": "Content-Length", 
     "Value": [ 
      "359" 
     ] 
     }, 
     { 
     "Key": "Content-Type", 
     "Value": [ 
      "application/json; charset=utf-8" 
     ] 
     }, 
     { 
     "Key": "Expires", 
     "Value": [ 
      "-1" 
     ] 
     } 
    ] 
    }, 
    "StatusCode": 200, 
    "ReasonPhrase": "OK", 
    "Headers": [ 
    { 
     "Key": "Pragma", 
     "Value": [ 
     "no-cache" 
     ] 
    }, 
    { 
     "Key": "X-SourceFiles", 
     "Value": ["..." 
     ] 
    }, 
    { 
     "Key": "Cache-Control", 
     "Value": [ 
     "no-cache" 
     ] 
    }, 
    { 
     "Key": "Date", 
     "Value": [ 
     "Thu, 21 Jul 2016 13:25:54 GMT" 
     ] 
    }, 
    { 
     "Key": "Server", 
     "Value": [ 
     "Microsoft-IIS/10.0" 
     ] 
    }, 
    { 
     "Key": "X-AspNet-Version", 
     "Value": [ 
     "4.0.30319" 
     ] 
    }, 
    { 
     "Key": "X-Powered-By", 
     "Value": [ 
     "ASP.NET" 
     ] 
    } 
    ], 
    "RequestMessage": { 
    "Version": { 
     "Major": 1, 
     "Minor": 1, 
     "Build": -1, 
     "Revision": -1, 
     "MajorRevision": -1, 
     "MinorRevision": -1 
    }, 
    "Content": null, 
    "Method": { 
     "Method": "GET" 
    }, 
    "RequestUri": "...", 
    "Headers": [ 
     { 
     "Key": "Accept", 
     "Value": [ 
      "application/json" 
     ] 
     } 
    ], 
    "Properties": {} 
    }, 
    "IsSuccessStatusCode": true 
} 

但normaly,該API返回該值(xml格式):

<License> 
    <Existing> 
    <Amount>0</Amount> 
    <Quantity>0</Quantity> 
    <UnitAmount>0</UnitAmount> 
    </Existing> 
<New> 
    <Amount>400</Amount> 
    <Quantity>50</Quantity> 
    <UnitAmount>8</UnitAmount> 
</New> 
<Result> 
    <Amount>400</Amount> 
    <Quantity>50</Quantity> 
    <UnitAmount>8</UnitAmount> 
</Result> 
</License> 
    <PartnerDiscount i:nil="true"/> 
</CalculatorResult> 

哪有我從API請求中提取這些值。如果我從API響應中執行"RequestUri",我會得到確切的數據。 我錯過了什麼?

+2

你不想序列化整個響應,只是'response.Content'。在這種情況下,這顯然是空的,所以其他的東西一定是錯的... –

+0

你可能會嘗試http://stackoverflow.com/a/15936272/423105 – LarsH

+0

當我使用response.Content我得到這個值: {「 Headers「:[{」Key「:」Content-Length「,」Value「:[」359「]},{」Key「:」Content-Type「,」Value「:[」application/json; charset = utf -8「]},{」Key「:」Expires「,」Value「:[」 - 1「]}]} – eg16

回答

4

你需要提取響應的正文,並返回

public class TestController : ApiController 
{ 
    [HttpGet] 
    public async Task<IHttpActionResult> TestMethod(...) 
    { 
     string _apiUrl = String.Format("..."); 
     string _baseAddress = "..."; 

     using (var client = new HttpClient()) 
     { 
      client.BaseAddress = new Uri(_baseAddress); 
      client.DefaultRequestHeaders.Accept.Clear(); 
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
      var responseMessage = await client.GetAsync(_apiUrl); 

      if (responseMessage.IsSuccessStatusCode) 
      { 
       var response = Request.CreateResponse(HttpStatusCode.OK); 
       response.Content = responseMessage.Content; 
       return ResponseMessage(response); 
      } 
     } 
     return NotFound(); 
    } 
} 

下面是使用ExpandoObject另一種選擇。

if (responseMessage.IsSuccessStatusCode) 
{ 
    var data = await responseMessage.Content.ReadAsAsync<ExpandoObject>(); 
    return Json(data); 
} 
+0

響應是XML。您不想返回JSON編碼的XML字符串。 – CodeCaster

+0

@CodeCaster你不知道這個事實。請求標頭正在請求JSON。 – toadflakz

+0

噢,沒錯,我沒有閱讀OP的序列化HTTP響應,我只看到了示例XML。 – CodeCaster