APIからデータを取得するために使用するコントローラーとメソッドを作成しました。 HttpResponseMessage
を使用して応答を取得します。すべてが正常に動作しますが、JSON形式で必要な正確なデータを取得できません。
メソッドは以下のようになります:
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
}
しかし、通常、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リクエストからこれらの値を抽出する方法を教えてください。"RequestUri"
from 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"));
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);
}