Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpClient returns The server committed a protocol violation - Browser returns correct data

Tags:

c#

webclient

I am trying to setup github with membership reboot.

If I try calling the github api from a browser like

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

I can see all the 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 of

The server committed a protocol violation. Section=ResponseStatusLine

What doesn't HttpClient like about what I am trying? Do I need to provide extra details that the browser is doing for me?

Any help would be appreciated!

like image 569
Diver Dan Avatar asked Oct 15 '14 01:10

Diver Dan


1 Answers

Adding

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

to my web.config seems to solve the problem...I dont really understand why I needed to add this but it works.

like image 55
Diver Dan Avatar answered Nov 10 '22 00:11

Diver Dan