Gus
Gus

Reputation: 1572

How to programmatically download GitHub data using C#

The NY Times is maintaining a GitHub repository at https://github.com/nytimes/covid-19-data. This repository contains License, ReadMe, and two data files. The data files are us-states.csv and us-counties.csv. Both contain a time-series collection of the daily number of cases and deaths of COVID-19 by state or county.

I am trying to download the us-states.csv file. The program that I developed is:

using System;
using System.Net;

namespace NYTimes_Console
    {

    // ************************************************* class Program

    class Program
        {

        // ****************************************************** Main

        static void Main ( string [ ] args )
            {
            byte [ ] bytes; 
            string   url = 
                        "https://raw.githubusercontent.com/nytimes/" + 
                        "covid-19-data/blob/master/us-states.csv";
            //string   url = 
            //            "https://github.com/nytimes/" + 
            //            "covid-19-data/blob/master/us-states.csv";


            try
                {
                using ( WebClient client = new WebClient ( ) )
                    {
                    client.Headers.Add("user-agent", "Anything");
                    bytes = client.DownloadData ( url );
                    }
                Console.WriteLine ( "Download Successful" );
                }
            catch ( Exception ex )
                {
                Console.WriteLine ( "Download Failed\n{0}",
                                    ex.Message.ToString ( ) );
                }
            Console.Write ( "Enter to exit");
            Console.ReadLine ( );
            }

        } // class Program

    } // namespace NYTimes_Console

When the url is "https://github.com/nytimes/covid-19-data/blob/master/us-states.csv", I receive the following:

Download Failed
The underlying connection was closed: An unexpected error occurred on a send.

When I look into the exception, I find:

InnerException = {"Received an unexpected EOF or 0 bytes from the transport stream."}

When the url is "https://raw.githubusercontent.com/nytimes/covid-19-data/blob/master/us-states.csv", I receive the following:

Download Failed
The remote server returned an error: (404) Not Found.

and the InnerException is null.

I appear to misunderstand the GitHub repositories and access to them.

Upvotes: 2

Views: 1899

Answers (1)

You can try to solve it like this:

string url = "https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv";

Upvotes: 3

Related Questions