Last active
December 10, 2020 23:24
-
-
Save GeoffCox/c0ba0a842bbe22c9215e to your computer and use it in GitHub Desktop.
Call GitHub REST API using HttpClient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WebRequestHandler handler = new WebRequestHandler() | |
{ | |
UseProxy = true, | |
}; | |
var client = new HttpClient(handler); | |
client.BaseAddress = new Uri("https://api.github.com/"); | |
// You should set the version so that GitHub knows what API you area calling | |
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json")); | |
// You must set a user agent so that the CRLF requirement on the header parsing is met. | |
// Otherwise you will get an excpetion message with "The server committed a protocol violation. Section=ResponseStatusLine" | |
client.DefaultRequestHeaders.UserAgent.Add(new System.Net.Http.Headers.ProductInfoHeaderValue("Mozilla", "5.0")); | |
// Set this to the URL you want. | |
var response = client.GetAsync("users/bellacode/repos").Result; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment