Reputation: 173
I am trying to use a WEB API for a personal learning project. The code is successful when trying to get a response from the server, but I can't get the body out of the response.
I am supposed to be getting something like:
{
"start_time": "2017-12-21T11:03:52Z",
"players": 27721,
"server_version": "1223575"
}
but I'm getting this instead:
StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Content-Type,Authorization,X-User-Agent
Access-Control-Allow-Methods: GET,OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Content-Type,Warning,X-Pages,X-ESI-Error-Limit-Remain,X-ESI-Error-Limit-Reset
Access-Control-Max-Age: 600
Strict-Transport-Security: max-age=31536000
X-Esi-Error-Limit-Remain: 100
X-Esi-Error-Limit-Reset: 54
Alt-Svc: clear
Cache-Control: public
Date: Thu, 21 Dec 2017 20:38:06 GMT
Via: 1.1 google
Content-Length: 80
Content-Type: application/json
Expires: Thu, 21 Dec 2017 20:38:34 GMT
Last-Modified: Thu, 21 Dec 2017 20:38:04 GMT
}
Here's my code:
static HttpClient client = new HttpClient();
static void Main(string[] args)
{
client.BaseAddress = new Uri("https://esi.tech.ccp.is/latest/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
GetTaskAsync("status/?datasource=tranquility").Wait();
}
static async Task GetTaskAsync(String endpoint)
{
HttpResponseMessage response = await client.GetAsync(client.BaseAddress + endpoint);
Console.WriteLine(response);
}
Upvotes: 2
Views: 957
Reputation: 10844
static HttpClient client = new HttpClient();
static void Main(string[] args)
{
client.BaseAddress = new Uri("https://esi.tech.ccp.is/latest/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
GetTaskAsync("status/?datasource=tranquility").Wait();
}
static async Task GetTaskAsync(String endpoint)
{
HttpResponseMessage response = await client.GetAsync(client.BaseAddress + endpoint);
if (response.IsSuccessStatusCode)
{
var data = await response.Content.ReadAsStringAsync();
Console.WriteLine(data);
}
}
Upvotes: 1
Reputation: 887195
Call response.Content.ReadAsStringAsync()
to read the body from the response.
Upvotes: 1