Skip to content

Protocol Violation Exception #2

Open
@abarrafo

Description

@abarrafo

There is one problem with the sample application at CSharp.GitHub / CSharp.GitHub.MVC 3 Example / Controllers / GitHubController.cs:

if (accessGrant != null)
        {
            //Until explict GitHub endpoints have API bindings, the following
            //method can be employed to consume GitHub endpoints
            var gitHubClient = _gitHubProvider.GetApi(accessGrant.AccessToken);
            var result = gitHubClient.RestOperations.GetForObject<JsonValue>("https://api.github.com/user/repos");

            ViewBag.AccessToken = accessGrant.AccessToken;
            ViewBag.ResultText = result.ToString();

            return View();
        }

This is throwing the following Exception:

"System.Net.WebException: The server committed a protocol violation. Section=ResponseStatusLine"

This is happening because GitHub requires a User-Agent header along with the request. To solve this problem is very easy.

  1. I upgraded to the latest Spring.Social.Core package, which is 4.3.0 as of this writing.

  2. I then modified the above code to an exchange() rather than a GetForObject()

        if(accessGrant != null)
        {
            HttpHeaders headers = new HttpHeaders();
            headers.Set("User-Agent", "{Some agent name}");
            HttpEntity entity = new HttpEntity(headers);
            var gitHubClient = _gitHubProvider.GetApi(accessGrant.AccessToken);
            var result = gitHubClient.RestOperations.Exchange<JsonValue>("https://api.github.com/user", HttpMethod.GET, entity);
            ViewBag.AccessToken = accessGrant.AccessToken;
            ViewBag.ResultText = result.ToString();
    
            return View();
    
        }
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions