简体   繁体   English

使用SSIS和C#下载多个文件

[英]Downloading multiple files using SSIS and C#

I have a requirement to download multiple files using SSIS and C# as the scripting language. 我需要使用SSIS和C#作为脚本语言下载多个文件。 I have tried but the file is downloading with 0 bites and I get an error message "the remote server returned and error(0) bad request. Can anyone please assist.This is what I have tried so far. I've pasted the code below. 我已经尝试过,但是文件正在以0比特下载,并且我收到一条错误消息“远程服务器已返回,错误(0)错误的请求。任何人都可以提供帮助。这是我到目前为止已经尝试过的。我已经粘贴了代码下面。

public void Main()
{

    string url = (string)Dts.Variables["User::vlink"].Value;
    string path = (string)Dts.Variables["$Project::DownloadPath"].Value;
    string filenames = (string)Dts.Variables["User::vfilename"].Value;
    string docpath = path + "\\" + filenames;

    if (!Directory.Exists(path))
    {
        DirectoryInfo di = Directory.CreateDirectory(path);
    }

    { 
        WebClient client = new WebClient();
        Uri uri = new Uri(url);

        client.Headers.Add("Accept-Language", " en-US");
        client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
        client.Headers.Add("Accept", "text/plain");
        client.Headers["Content-Type"] = "text/plain;charset=UTF-8";
        client.Headers.Add("Accept", " text/html, application/xhtml+xml, */*");
        client.Headers.Add("UserAgent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0");
        client.Headers["Accept-Encoding"] = "application/x-gzip";
        client.Credentials = new NetworkCredential("Test", "Test@1234", "Test");
        client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileComplete);
        client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(Client_DownloadProgressChanged);

        client.DownloadFileAsync(uri, docpath);



        //while (client.IsBusy)
        //{
        //    System.Threading.Thread.Sleep(1000);
        //}


        //Dts.TaskResult = (int)ScriptResults.Success;
        //MessageBox.Show("Download Complete");
    }

}

private void Client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    Console.WriteLine(e.BytesReceived + " " + e.ProgressPercentage);

}

void client_DownloadFileComplete(object sender, AsyncCompletedEventArgs e)
{
    if (e.Error != null)
        MessageBox.Show(e.Error.Message);
    else
        MessageBox.Show("Download Completed");
}


#region ScriptResults declaration
/// <summary>
/// This enum provides a convenient shorthand within the scope of this class for setting the
/// result of the script.
/// 
/// This code was generated automatically.
/// </summary>
enum ScriptResults
{
    Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
    Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};

#endregion

I noticed that the file was downloading with 0 bites because the login page was blocking the script and the link was different once you login. 我注意到该文件的下载速度为0位,因为登录页面阻止了脚本,并且登录后链接有所不同。 The link was therefore not being passed correctly because after login, all the white spaces were being replaced "%" sign. 因此,该链接未正确传递,因为登录后,所有空格都被替换为“%”符号。 So I had to replace all the white spaces with with the same. 所以我必须用相同的空格替换所有空白。

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

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