簡體   English   中英

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

[英]C# DownloadFileTaskAsync not downloading nested zip file

請幫助C#新手,

我正在嘗試使用SSIS中的腳本任務從HTTPS站點下載壓縮文件。 每個“外部” zip文件都包含“內部” zip文件,其中包含3個txt文件。

經過廣泛的搜索,我使用“ await”,“。Wait()”甚至“ while(webClient.IsBusy)”增強了DownloadFileTaskAsync,但仍然設法僅下載空的“外部” 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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM