Error consuming API GitHub V3 using HttpClient or even HttpWebRequest

1

I am not able to consume the GitHub V3 API with basic authentication (without using Octokit). I know it works with RestSharp plus I want to know why it does not work with HttpClient and HttpWebRequest .

I always get the same answer:

  

"A protocol violation error occurred"

Here is my code:

using (var client = new HttpClient())
{
    var response = client.GetAsync("https://api.github.com/emojis").Result;

    if (response.IsSuccessStatusCode)
    {
        var responseContent = response.Content; 
        string responseString = responseContent.ReadAsStringAsync().Result;

        Console.WriteLine(responseString);
    }
}
    
asked by anonymous 06.05.2017 / 15:37

1 answer

0

To use HttpClient I recommend using async and await for calls, which makes code efficient for outside calls, I ran a test and it worked, it follows the code, remembering that it was necessary to inform a Header.

public async Task GetGithub(){
      using (var client = new HttpClient())
      {
        client.DefaultRequestHeaders.Add("User-Agent", "Other");

        var response = await client.GetAsync("https://api.github.com/emojis");

        if (response.IsSuccessStatusCode)
        {
            var responseContent = response.Content; 
            string responseString = await responseContent.ReadAsStringAsync();

            Console.WriteLine(responseString);
        }
    }
}
    
06.05.2017 / 16:58