简体   繁体   English

C#DownloadFileTaskAsync不下载嵌套的zip文件

[英]C# DownloadFileTaskAsync not downloading nested zip file

Please help to a C# newbie, 请帮助C#新手,

I'm trying to download zipped files from HTTPS site with Script task in SSIS. 我正在尝试使用SSIS中的脚本任务从HTTPS站点下载压缩文件。 Each "external" zip file contains "internal" zip file, that contains 3 txt files. 每个“外部” zip文件都包含“内部” zip文件,其中包含3个txt文件。

After extensive search, i've enhanced the DownloadFileTaskAsync with "await", ".Wait()" and even "while (webClient.IsBusy) but still manage to only download the empty "external" zip file. 经过广泛的搜索,我使用“ await”,“。Wait()”甚至“ while(webClient.IsBusy)”增强了DownloadFileTaskAsync,但仍然设法仅下载空的“外部” zip文件。

Please help me to find a way to download full "external" file in a way it won't be empty, but will contain the "internal" zip and all 3 txt files inside of it: 请帮助我找到一种下载完整“外部”文件的方法,该方法不会为空,但其中包含“内部” 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;
    }

> >

Try this one, add some headers to webClient: 试试这个,向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;
}

and use this class instead of WebClient: 并使用此类代替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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM