我的C#中的脚本有问题。我已经尝试在GitHub中列出所有公共存储库。
这是我的代码:
HttpWebRequest request = WebRequest.Create("https://api.github.com/repositories?since=364") as HttpWebRequest;
request.Method = "GET";
request.Proxy = null;
request.ContentType = "application/json";
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
content1= reader.ReadToEnd();
}
我有错误:
System.Net.WebException was unhandled by user code
The server committed a protocol violation. Section=ResponseStatusLine
Source=System
An exception of type 'System.Net.WebException' occurred in System.dll but was not handled in user code
使用....
感谢您的帮助。
答案 0 :(得分:0)
HttpWebRequest request = WebRequest.Create("https://api.github.com/repositories?since=364") as HttpWebRequest;
request.UserAgent = "TestApp";
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
content1= reader.ReadToEnd();
}
使用UserAgent,因为当您从浏览器请求时,您的URL会提供数据,但是当应用程序不是服务器时,UserAgent就会出现问题。虽然您也可以看到相同的错误,但其他一些参数也会丢失。