我正在为我的应用程序从 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/