c# - 如何使用 C# 以编程方式下载 GitHub 数据

标签 c# github download

《纽约时报》正在维护一个 GitHub 存储库,地址为 https://github.com/nytimes/covid-19-data 。该存储库包含许可证、自述文件和两个数据文件。数据文件为 us-states.csv 和 us-counties.csv。两者都包含按州或县划分的每日 COVID-19 病例数和死亡人数的时间序列集合。

我正在尝试下载 us-states.csv 文件。我开发的程序是:

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

当网址为“https://github.com/nytimes/covid-19-data/blob/master/us-states.csv ”时,我收到以下内容:

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

当我查看异常时,我发现:

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

当网址为“https://raw.githubusercontent.com/nytimes/covid-19-data/blob/master/us-states.csv ”时,我收到以下内容:

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

并且 InnerException 为 null。

我似乎误解了 GitHub 存储库以及对它们的访问。

最佳答案

你可以尝试这样解决:

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

关于c# - 如何使用 C# 以编程方式下载 GitHub 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61037876/

相关文章:

c# - .NET CF 为组合框设置 'DroppedDown'

c# - 在 C# Web 表单中单击 [确定] 后,如何在 .aspx 页面之间传递变量

c# - C# 检查Excel文件是否为空

open-source - GitHub开源项目工作流程

ios - Xcode 7 GM 无法验证 git 存储库

git - 如何在 Github Desktop 中将 fork 的仓库与原始仓库同步

c# - 系统.Net.WebException : The remote server returned an error: (403) Forbidden when access Google API

qt - 使用wkhtmltopdf时如何处理ContentNotFoundError?

unit-testing - 包含测试的应用程序示例

python - 将文件返回给 WSGI GET 请求