2013-01-11 350 views
3

我收到此錯誤「遠程服務器返回錯誤:(530)未登錄。」同時使用FtpWebRequest上傳文件。只有當我將文件轉移到其子文件夾的路徑發生
FtpWebRequest返回「遠程服務器返回錯誤:(530)未登錄」

  1. 錯誤,否則它完全工作正常。
  2. 上傳大約5到10 MB的大文件時,超時。

    void FtpTransfer(string siteName, string portNumber, string ftpUser, string ftpPassword, string destPath) 
    { 
        FtpWebRequest request; 
        DateTime now = DateTime.Now; 
        string now_string = 
         (now.Year).ToString() 
         + "_" + 
         (now.Month).ToString("0#") 
         + "_" + 
         (now.Day).ToString("0#"); 
    
        foreach (object item in listBox1.Items) 
        { 
         string srcFile = item.ToString(); 
         lblSource.Text = srcFile; 
         Uri uri = new Uri(srcFile); 
         string destFile = srcFile.Replace(lblPath.Text, "").Replace("\\\\", "\\").Replace("\\", "/").Replace("www/",""); 
    
         Configuration oConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
         int timeout = int.Parse(oConfig.AppSettings.Settings["TimeOut"].Value); 
    
         if (siteName == "mysite1.co.in" || siteName == "sd1.mysite2.net") 
          destFile = "ftp://" + siteName + ":" + portNumber + "/" + siteName + "/_test" + destFile; //error here 
         else 
          destFile = "ftp://" + siteName + ":" + portNumber + "/" + siteName + destFile; //no error 
         lblDestn.Text = destFile; 
    
         request = (FtpWebRequest)WebRequest.Create(destFile); 
         request.Credentials = new NetworkCredential(ftpUser, ftpPassword); 
         request.Timeout = 6000; 
         request.Method = WebRequestMethods.Ftp.UploadFile; 
         request.UsePassive = true; 
         request.UseBinary = true; 
         request.KeepAlive = true; 
    
         // Copy the contents of the file to the request stream. 
         StreamReader sourceStream = new StreamReader(@srcFile); 
         byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); 
         sourceStream.Close(); 
         request.ContentLength = fileContents.Length; 
    
         Stream requestStream = request.GetRequestStream(); 
         requestStream.Write(fileContents, 0, fileContents.Length); 
         requestStream.Close(); 
    
         FtpWebResponse response = (FtpWebResponse)request.GetResponse(); 
    
         string path = System.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]); 
         System.IO.StreamWriter w = System.IO.File.AppendText(path + "\\log_" + now_string + ".txt"); 
         w.WriteLine(DateTime.Now.ToString("yyy-MM-dd HH:mm:ss") 
          + " " 
          + srcFile 
          + " " 
          + destFile 
          + " " 
          + response.StatusDescription); 
    
         w.Close(); 
    
         response.Close(); 
        } 
    

回答

0

我假設你已經嘗試通過一個標準的FTP客戶端來完成相同的操作。如果沒有,請嘗試。然後我會使用Wireshark來確保這是來自服務器的響應,並確認正在發送credentails。在驗證之後,請檢查FTP所有者並確保服務器配置正確。

+0

它使用FileZilla的工作完全正常。 – vasanthkumar

+0

那麼,你有沒有wireshark你的代碼運行? –

+0

是的,我做過。雖然我不是使用wireshark的專家,但是我看到了正確發送的用戶名和密碼以及從服務器返回的相同響應。 – vasanthkumar

-2

我不完全知道你的問題的解決方案。但一些建議:

儘可能在IDisposable上使用using(...)。這可以在您完成後促進適當的資源釋放和清理。 MSDN: Using

您使用的超時時間爲6000毫秒,您是否應該增加它對於大文件或使用本地變量timeout(從您的應用程序設置讀取)。

改進代碼using

private void FtpTransfer(string siteName, string portNumber, string ftpUser, string ftpPassword, string destPath) 
     { 
      DateTime now = DateTime.Now; 
      string now_string = 
       (now.Year).ToString() 
       + "_" + 
       (now.Month).ToString("0#") 
       + "_" + 
       (now.Day).ToString("0#"); 

      foreach (object item in listBox1.Items) 
      { 
       string srcFile = item.ToString(); 
       lblSource.Text = srcFile; 
       Uri uri = new Uri(srcFile); 
       string destFile = srcFile.Replace(lblPath.Text, "").Replace("\\\\", "\\").Replace("\\", "/").Replace("www/", ""); 

       Configuration oConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
       int timeout = int.Parse(oConfig.AppSettings.Settings["TimeOut"].Value); 

       if (siteName == "mysite1.co.in" || siteName == "sd1.mysite2.net") 
        destFile = "ftp://" + siteName + ":" + portNumber + "/" + siteName + "/_test" + destFile; //error here 
       else 
        destFile = "ftp://" + siteName + ":" + portNumber + "/" + siteName + destFile; //no error 
       lblDestn.Text = destFile; 

       FtpWebRequest request = (FtpWebRequest)WebRequest.Create(destFile); 
       request.Credentials = new NetworkCredential(ftpUser, ftpPassword); 
       request.Timeout = 6000; 
       request.Method = WebRequestMethods.Ftp.UploadFile; 
       request.UsePassive = true; 
       request.UseBinary = true; 
       request.KeepAlive = true; 

       // Copy the contents of the file to the request stream. 
       byte[] fileContents; 
       using (StreamReader sourceStream = new StreamReader(@srcFile)) 
       { 
        fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); 
       } 
       request.ContentLength = fileContents.Length; 

       using (Stream requestStream = request.GetRequestStream()) 
       { 
        requestStream.Write(fileContents, 0, fileContents.Length); 
       } 

       using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) 
       { 
        string path = System.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]); 
        System.IO.StreamWriter w = System.IO.File.AppendText(path + "\\log_" + now_string + ".txt"); 
        w.WriteLine(DateTime.Now.ToString("yyy-MM-dd HH:mm:ss") 
           + " " 
           + srcFile 
           + " " 
           + destFile 
           + " " 
           + response.StatusDescription); 

       } 
      } 

     } 
+0

響應沒有區別。我仍然得到同樣的錯誤。關於時間,無論是6000或600000,它的迴應都是一樣的。 – vasanthkumar

+0

好的。某些FTP服務器實現有一個超時,也許你必須發送'NOP'命令到服務器。在上傳/下載期間保持連接處於活動狀態。 –

+0

您能否提供示例代碼來檢查NOOP命令? – vasanthkumar

相關問題