跟进在 C# 中下载该网站使用 cookie 的文件

Follow up for Download a File in C# that website uses cookies

这是 的跟进。

我觉得问题已得到解答,但我需要更多帮助。

这是我当前的代码:

    public async void TryDownload()
    {
        var clientHandler = new HttpClientHandler
        {
            AllowAutoRedirect = true,
            UseCookies = true,
            CookieContainer = new CookieContainer()
        };

        using (var httpClient = new HttpClient(clientHandler))
        {
            // this gets the request and allows the site to set cookies.
            var warmup = await httpClient.GetAsync("https://www.fapiis.gov/fapiis/allfapiisdata.action"); //This is the last line that runs when I step thru it.

            // get the file (cookies are sent automatically).
            var fileResponse = httpClient.GetAsync("https://www.fapiis.gov/fapiis/downloadview?type=allFapiis");

            if (fileResponse.Result.IsSuccessStatusCode)
            {
                HttpContent content = fileResponse.Result.Content;
                var contentStream = await content.ReadAsStreamAsync();

                string fname = "allFapiis" + DateTime.Now.ToLongDateString() + ".xlsx";
                using (var fileStream = System.IO.File.Create(@"C:\ERA\DATA\FAPIIS\" + fname))
                {
                    contentStream.CopyTo(fileStream);
                }
            }
        }
    }    

    public void Main()
    {
        // TODO: Add your code here

        TryDownload();

        Dts.TaskResult = (int)ScriptResults.Success;
    }

我在 SSIS 的脚本任务中运行这个。

当我单步执行它时,该过程突然以 "warmup" 的设置结束。

我可以看到该过程对回答者有效,因为我可以看到他的输出与我手动下载的相匹配。

如果我尝试将 await 合并到来自 main 的 TryDownload() 调用中,它会大声说需要在异步任务方法中,但我不能这样做,因为这是在 main 中。

我做错了什么?

感谢您的帮助!

TryDownload 应该是 Task。那你就等着吧。

public async Task TryDownload()

public static async void Main()
{
    await TryDownload();
}