保存Web请求中的文件

时间:2014-12-03 17:02:37

标签: ssis

我正在尝试将旧的Perl脚本转换为SSIS包,其中包括从网站获取zip文件。站点的URL是一个JSP页面,它在查询字符串中包含许多值。将URL放入浏览器会导致页面自动下载文件,无需用户交互。

我花了好几个小时试图在SSIS中复制这个没有运气!我尝试过WebClient类,包括OpenRead,UploadValues和DownloadFile方法,以及QueryString或NameValueCollection的各种组合。最终结果始终是“远程服务器返回错误:(400)错误请求。”

关于如何让它发挥作用的任何建议?

感谢。

1 个答案:

答案 0 :(得分:0)

我在SSIS中使用稍微修改过的C#脚本(可能在这里)来执行此操作。如果有人认出这段代码,我很乐意撤回这个答案。

现在,我不是C#开发者,所以如果这段代码太可怕了,我就恳求无知。它确实从网站下载文件,但这就是我需要它做的全部。

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
using System.Text;
using System.Net;



public void Main()
        {
            //Init Variables
            string downloadurl = Dts.Variables["User::downloadlink"].Value.ToString();
            string targetdir = Dts.Variables["User::targetdir"].Value.ToString();
            string targetfile = Dts.Variables["User::targetfilename"].Value.ToString();
            bool fileexists = File.Exists(targetdir + targetfile);
            bool overwrite = Convert.ToBoolean(Dts.Variables["User::overwritedlfile"].Value);
            string webpassword = "";
            string weblogin = "";
        try
        {



            // Logging start of download
            bool fireAgain = true;
            Dts.Events.FireInformation(0, "Download File", "Start downloading " + Dts.Variables["User::downloadlink"].Value.ToString(), string.Empty, 0, ref fireAgain);

            // For authenticated downloads

            if ((webpassword != "" || weblogin != "") && (fileexists == false || (overwrite == true && fileexists == true)))
            {
                weblogin = Dts.Variables["User::weblogin"].Value.ToString();
                webpassword = Dts.Variables["User::webpassword"].Value.ToString();
                // Create a webclient to download a file
                using (WebClient mySSISWebClient = new WebClient())
                {
                    //Insert credentials
                    mySSISWebClient.Credentials = new NetworkCredential(weblogin, webpassword);
                    //Download file (source, destination)
                    mySSISWebClient.DownloadFile(downloadurl, targetdir + targetfile);
                }

                //// Logging end of download
                Dts.Events.FireInformation(0, "Download File", "Finished downloading " + Dts.Variables["User::downloadlink"].Value.ToString(), string.Empty, 0, ref fireAgain);
                //Console.WriteLine("Download Complete");


                // Quit Script Task succesful
                Dts.TaskResult = (int)ScriptResults.Success;
            }
            // For regular downloads
            if (fileexists == false || (overwrite == true && fileexists == true))
            {

                // Create a webclient to download a file
                using (WebClient mySSISWebClient = new WebClient())
                {
                    mySSISWebClient.DownloadFile(downloadurl, targetdir + targetfile);
                }

                //// Logging end of download
                Dts.Events.FireInformation(0, "Download File", "Finished downloading " + Dts.Variables["User::downloadlink"].Value.ToString(), string.Empty, 0, ref fireAgain);
                //Console.WriteLine("Download Complete");


                // Quit Script Task succesful
                Dts.TaskResult = (int)ScriptResults.Success;
            }

            else
            {
                Dts.Events.FireError(0, "Download File", "Download failed: " + (targetfile + " already exists in the target directory"), string.Empty, 0);
                Dts.TaskResult = (int)ScriptResults.Failure;
                throw new Exception(targetfile + " already exists in the target directory");


            }

        }
        catch (Exception ex)
        {
            // Logging why download failed
            Dts.Events.FireError(0, "Download File", "Download failed: " + ex.Message, string.Empty, 0);

            //Console.WriteLine(ex.ToString());

            // Quit Script Task unsuccesful
            Dts.TaskResult = (int)ScriptResults.Failure;
        }