c# - 使用 Github api 违反协议(protocol)

标签 c# .net api github

我正在为我的应用程序从 Github 获取数据。 前两个 OAuth 步骤没问题,但在第三个步骤中出现以下错误: “服务器违反了协议(protocol)。Section=ResponseStatusLine ERROR” 这是我的代码:

protected String WebRequest(string url)
    {
        url += (String.IsNullOrEmpty(new Uri(url).Query) ? "?" : "&") + "access_token=" + _accessToken;
        HttpWebRequest webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
        webRequest.Method = "GET";
        webRequest.ServicePoint.Expect100Continue = false;
        try
        {
            using (StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
                return responseReader.ReadToEnd();
        }
        catch
        {
            return String.Empty;
        }
    }

程序在使用 StreamReader 对象后出现异常,返回错误。 如果我按照这些说明 The server committed a protocol violation. Section=ResponseStatusLine ERROR ,错误变成“403 forbidden”。 Github使用api V2的时候,和现在不同,这段代码是没有问题的。 所以,不能是 .NET 限制,而是与 Github 服务器相关的东西。 有什么建议吗?

最佳答案

您需要像这样设置 UserAgent:

webRequest.UserAgent = "YourAppName";

否则会给出The server committed a protocol violation。 Section=ResponseStatusLine 错误。

关于c# - 使用 Github api 违反协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22649419/

相关文章:

c# - .jar 和 .dll 文件之间的区别

javascript - 如何显示当前 Twitch 流的状态?

javascript - 使用 nodeJS 开发 API

ios - 在SWIFT中解析JSON API到WIFI和4G的不同结果

c# - C# 中的多个串行端口/使用列表 <> 时出现问题

c# - 处理异步函数的空响应的正确方法是什么?

c# - 即将到期的 Lazy<T> 类

c# - 将对象转换为多维字符串数组

c# - 使用 MVVM 管理可取消的部分更新

c# - 验证一个方法被调用