2014-12-03 60 views
0

我试图将旧的Perl脚本转换为SSIS包,其中包括从网站获取zip文件。该网站的URL是一个JSP页面,它在查询字符串中获取多个值。将URL放入浏览器会使页面自动下载文件,无需用户交互。保存网络请求中的文件

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

关于如何让这个工作的任何建议?

谢谢。

+0

通常,如果您实际发布了一些代码,则会得到更好的响应。 – 2014-12-03 17:07:26

回答

0

我使用稍微修改的C#脚本,我发现(可能在这里)在SSIS做到这一点。如果有人认出这段代码,我会很乐意撤回这个答案。

现在,我不是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; 
     } 
+0

同样的结果:(,“远程服务器返回一个错误:(400)错误的请求。”。 – DR74 2014-12-03 17:35:37

+0

URL中是否有两个冒号(:)? – 2014-12-03 19:19:18