【问题标题】:C# downloading a csv file from https siteC# 从 https 站点下载 csv 文件
【发布时间】:2021-01-11 18:16:07
【问题描述】:

我正在尝试将文件下载到我的应用程序中。从我可以看到如果我将此链接放入浏览器中,我会下载一个很好的文件,所以我知道该文件可供我获取。我还有其他文件要从不同的站点下载,它们都运行良好,但这个文件不会以可用的方式为我下载。我想我不明白一些事情,你知道我遗漏的关键事实吗?

经过多次尝试,我现在编写了以下代码

 private string url =
        "https://coronavirus.data.gov.uk/api/v1/data?filters=areaType=nation&structure={\"Name\":\"areaName\",\"date\":\"date\",\"FirstDose\":\"cumPeopleVaccinatedFirstDoseByPublishDate\",\"SecondDose\":\"cumPeopleVaccinatedSecondDoseByPublishDate\"}&format=csv";

    private void btn_wc1_Click(object sender, EventArgs e)
    {
        WebClient wc = new WebClient();

        wc.Encoding = Encoding.UTF8;
        wc.DownloadFile(url, "wc1_uk_data.csv");
    }

    private void btn_wc2_Click(object sender, EventArgs e)
    {
        using (var webClient = new WebClient())
         {
             webClient.Encoding = Encoding.UTF8;
             string s = webClient.DownloadString(url);
             File.WriteAllText(@"wc2_uk_data.csv", s);
         }
    }

    private async void btn_https_Click(object sender, EventArgs e)
    {
        HttpClient _client = new HttpClient();
        byte[] buffer = null;
        try
        {
            HttpResponseMessage task = await _client.GetAsync(url);
            Stream task2 = await task.Content.ReadAsStreamAsync();
            using (MemoryStream ms = new MemoryStream())
            {
                await task2.CopyToAsync(ms);
                buffer = ms.ToArray();
            }
            File.WriteAllBytes("https_uk_data.csv", buffer);
        }
        catch
        {

        }
    }

    private void btn_wc3_Click(object sender, EventArgs e)
    {
        using (var webClient = new WebClient())
        {
            webClient.Encoding = Encoding.UTF8;
            byte[] myDataBuffer = webClient.DownloadData(url);
            MemoryStream ms = new MemoryStream(myDataBuffer);
            FileStream f = new FileStream(Path.GetFullPath(Path.Combine(Application.StartupPath, "wc3_uk_data.csv")),
            FileMode.OpenOrCreate);
            ms.WriteTo(f);
            f.Close();
            ms.Close();
        }
    }

使用以下用户界面

以上所有不同的功能都会下载一个文件,但没有一个下载的文件可用。似乎它不是文件,但可能与有关文件的信息有关。由于我的应用程序不知道该怎么做,我从不回复对方想要的东西。我想如果我回复了我得到的下一组数据就是文件。

如果我将 URL 放入浏览器,那么我会得到一个很好的文件。这个链接现在很好。

https://coronavirus.data.gov.uk/api/v1/data?filters=areaType=nation&structure={"Name":"areaName","date":"date","FirstDose":"cumPeopleVaccinatedFirstDoseByPublishDate","SecondDose":"cumPeopleVaccinatedSecondDoseByPublishDate"}&format=csv

有人知道我需要在我的应用中做什么才能像浏览器一样获取文件吗?

【问题讨论】:

标签: c# webclient


【解决方案1】:

在调用DownloadString之前需要设置WebClient.Encoding

string url = "https://coronavirus.data.gov.uk/api/v1/data?filters=areaType=nation&structure={\"Name\":\"areaName\",\"date\":\"date\",\"FirstDose\":\"newPeopleReceivingFirstDose\",\"SecondDose\":\"newPeopleReceivingSecondDose\"}&format=csv";
using (var webClient = new WebClient())
{
    webClient.Encoding = Encoding.UTF8;
    string s = webClient.DownloadString(url);
}

这是一个相关的问题: WebClient.DownloadString results in mangled characters due to encoding issues, but the browser is OK

【讨论】:

  • 谢谢,但还是不行。我尝试了链接coronavirus.data.gov.uk/api/v1/…{"Name":"areaName","date":"date","FirstDose":"cumPeopleVaccinatedFirstDoseByPublishDate","SecondDose":"cumPeopleVaccinatedSecondDoseByPublishDate"}&format=csv 但仍然没有带回来.csv 文件。我不确定它会带来什么,因为它与任何 UTF 或 Unicode 一样不可读。
猜你喜欢
  • 2017-08-27
  • 2012-10-18
  • 2016-12-29
  • 2017-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
相关资源
最近更新 更多