C# 下载文件任务异步不下载嵌套的 zip 文件
本文关键字:下载 文件 zip 嵌套 异步 任务 | 更新日期: 2023-09-27 17:57:24
请帮助C#新手,
我正在尝试使用 SSIS 中的脚本任务从 HTTPS 站点下载压缩文件。每个"外部"zip 文件都包含"内部"zip 文件,其中包含 3 个 txt 文件。
经过广泛的搜索,我用"await","增强了DownloadFileTaskAsync。Wait()"甚至"while(webClient.IsBusy)",但仍然设法只下载空的"外部"zip文件。
请帮助我找到一种方法来下载完整的"外部"文件,它不会是空的,而是包含"内部"zip 和其中的所有 3 个 txt 文件:
public async void Main()
{
WebClient webClient = new WebClient();
webClient.Credentials = new NetworkCredential("myuser", "password", "https://example.com/Login.htm");
webClient.DownloadFileTaskAsync(new Uri("https://example.com/#/FROMsender/EXTERNAL_20160706.zip"), @"C:'temp'Test'EXTERNAL_20160706.zip").Wait();
while (webClient.IsBusy) Thread.Sleep(1000);
Dts.TaskResult = (int)ScriptResults.Success;
}
>
试试这个,向webClient添加一些标头:
public async void Main()
{
WebClientEx webClient = new WebClientEx(); // <= use WebClientEx
webClient.Credentials = new NetworkCredential("myuser", "password", "https://example.com/Login.htm");
// Add headers here ------------
webClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36");
webClient.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
webClient.Headers.Add("Accept-Language", "en-US,en;q=0.5");
webClient.Headers.Add("Accept-Encoding", "gzip, deflate");
webClient.Headers.Add("Cache-Control", "max-age=0");
webClient.Headers.Add("DNT", "1");
//------------------------------
webClient.DownloadFileTaskAsync(new Uri("https://example.com/#/FROMsender/EXTERNAL_20160706.zip"), @"C:'temp'Test'EXTERNAL_20160706.zip").Wait();
while (webClient.IsBusy) Thread.Sleep(1000);
Dts.TaskResult = (int)ScriptResults.Success;
}
并使用此类而不是 WebClient:
public class WebClientEx : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
var webRequest = (HttpWebRequest) base.GetWebRequest(address);
if (webRequest != null)
{
webRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
}
return webRequest;
}
}