C#从https站点下载一个csv文件

问题描述

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

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

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

使用以下界面

Image of UI

上述所有不同的功能都会下载一个文件,但没有一个下载的文件可用。似乎它不是文件,但可能与有关文件的信息有关。由于我的应用程序不知道如何处理这个,我从不回复另一端想要的东西。我想如果我回复了我得到的下一组数据就是文件

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

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

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

解决方法

您需要在调用 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