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

时间:2018-03-22 11:32:48

标签: c# ssis

我需要使用SSIS和C#作为脚本语言下载多个文件。我已经尝试但是文件正在下载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

1 个答案:

答案 0 :(得分:1)

我注意到文件正在以0比特下载,因为登录页面阻止了脚本,并且登录后链接有所不同。因此,该链接未正确传递,因为登录后,所有空格都被替换为“%”符号。所以我必须用相同的空格替换所有空白。