Return HttpClient Server committed protocol violation - browser returns correct data

I am trying to configure github with a reboot membership.

If I try to call github api from a browser, for example

https://api.github.com/user?access_token=XXXXXXX

I can see all valid json data, however, if I try from .net

public ActionResult Index() { var url = "https://api.github.com/user?access_token=XXXXXXXX"; ////add additional params //if (additionalParams != null) //{ // foreach (string key in additionalParams) // { // url += string.Format("&{0}={1}", key, additionalParams[key]); // } //} HttpClient client = new HttpClient(); var result = client.GetAsync(url).Result; if (result.IsSuccessStatusCode) { var json = result.Content.ReadAsStringAsync().Result; var profile = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(json); //return GetClaimsFromProfile(profile); } return View(); } 

I get an error

The server committed a protocol violation. Section = ResponseStatusLine

What is not HttpClient, how about what I'm trying? Do I need to provide additional information that the browser does for me?

Any help would be appreciated!

+6
source share
2 answers

Adding

 <system.net> <settings> <httpWebRequest useUnsafeHeaderParsing="true" /> </settings> </system.net> 

to my web.config seems to solve the problem ... I really don't understand why I needed to add this, but it works.

+2
source

Not sure if this is relevant here, but I got the same problem on the endpoint https://api.github.com/user . My decision was simpler than I thought, but nonetheless annoying as it took me several hours to find out.

User-Agent must be passed to the API endpoint, perhaps it also works here by specifying the User-Agent .

0
source

Source: https://habr.com/ru/post/976753/


All Articles