When I am making HTTP requests in C# and reading the response, I am getting garbage data in the response. I am unable to figure out the problem. Consider the following example snippet where I make a call to the Stack Exchange API.
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://api.stackexchange.com/info");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("?site=stackoverflow").Result;
if (response.IsSuccessStatusCode)
{
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
}
else
{
Console.WriteLine("Sorry");
}
The screenshot below illustrates the garbage output I am receiving:
I'd appreciate any help in trying to debug this issue and knowing where I went wrong.