I would like to send curl request that I got from JS code below with HttpClient.
curl -v -X GET "hxxps://airport.api.aero/airport?user_key=[token]"
so I wrote the C# code
string response = await client.GetStringAsync(new Uri("hxxps://airport.api.aero/airport?user_key=[token]"));
and I tried to deserialize the response with the code below.
// deserialize the JSON object response, the information will become an AirportObject.RootObject instance
rootObject = JsonConvert.DeserializeObject<AirportObject.RootObject>(response);
rootObject is an instance of C# class object and the information received from the link is in JSON format. Therefore, it needs a conversion from JSON to an instance of C# class.
Also, when I tried to visit the link directly, it gave the JSON data encapsulated with callback() function.
Did I write it correct? When I run it, it did nothing.
Thanks.
[FIXED] so my problem was just not specifying the headers type. Here is the code fixing the program:
//set request headers to accept JSON data
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
This is how you need to get a response to deserialize it:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost/api/v1/");
HttpResponseMessage response = await client.GetAsync("some");
if(response.IsSuccessfulStatusCode)
{
// The entity is within the response's content
RootObject root = await response.Content.ReadAsAsync<RootObject>();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.