Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protocol Violation Exception #2

Open
abarrafo opened this issue Nov 10, 2015 · 0 comments
Open

Protocol Violation Exception #2

abarrafo opened this issue Nov 10, 2015 · 0 comments

Comments

@abarrafo
Copy link

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();
    
        }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant