I have created a Controller and method which i use to get data from an API. I use HttpResponseMessage
to get the response. Everything works fine but i can not get the exact data i want in JSON format.
Method looks like below:
我创建了一个Controller和方法,用于从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();
}
}
The response i get looks like:
我得到的回应如下:
{
"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
}
But normaly, the API return this value (xml
format):
但是,正常情况下,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>
How can i extract these values from the API request.If i execute the "RequestUri"
from API response i get the exact data. What am i missing?
如何从API请求中提取这些值。如果我从API响应执行“RequestUri”,我会得到确切的数据。我错过了什么?
1 个解决方案
#1
4
You need to extract the body of the response and return that
您需要提取响应的主体并返回该响应
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();
}
}
Here is another option using ExpandoObject
.
这是使用ExpandoObject的另一个选项。
if (responseMessage.IsSuccessStatusCode)
{
var data = await responseMessage.Content.ReadAsAsync<ExpandoObject>();
return Json(data);
}
#1
4
You need to extract the body of the response and return that
您需要提取响应的主体并返回该响应
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();
}
}
Here is another option using ExpandoObject
.
这是使用ExpandoObject的另一个选项。
if (responseMessage.IsSuccessStatusCode)
{
var data = await responseMessage.Content.ReadAsAsync<ExpandoObject>();
return Json(data);
}