2014-12-03 58 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