1

I have a problem with my script in C#. I've tried list all public repositories in GitHub.

Here is my code:

             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();

        }

And i have error:

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

on line with using....

Thanks for your help.

pwron
  • 13
  • 4

1 Answers1

1
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();
    }

Use UserAgent because when you request from Browser your url serve data but when application it is not server and UserAgent is problem. Though you can see same error with some other parameter missing as well.

dotnetstep
  • 17,065
  • 5
  • 54
  • 72