2011-01-05 38 views
2

我有以下c#代碼。它在針對.NET Framework 3.5或2.0編譯時運行得很好(我沒有針對3.0進行測試,但它很可能也會工作)。問題是,它在針對.NET Framework 4.0構建時失敗。Ftp僅在.NET 4.0中拋出WebException

 FtpWebRequest Ftp = (FtpWebRequest)FtpWebRequest.Create(Url_ + '/' + e.Name); 

     Ftp.Method = WebRequestMethods.Ftp.UploadFile; 
     Ftp.Credentials = new NetworkCredential(Login_, Password_); 
     Ftp.UseBinary = true; 
     Ftp.KeepAlive = false; 
     Ftp.UsePassive = true; 

     Stream S = Ftp.GetRequestStream(); 

     byte[] Content = null; 
     bool Continue = false; 
     do 
     { 
      try 
      { 
       Continue = false; 
       Content = File.ReadAllBytes(e.FullPath); 
      } 
      catch (Exception) 
      { 
       Continue = true; 
       System.Threading.Thread.Sleep(500); 
      } 
     } while (Continue); 

     S.Write(Content, 0, Content.Length); 
     S.Close(); 

     FtpWebResponse Resp = (FtpWebResponse)Ftp.GetResponse(); 
     if (Resp.StatusCode != FtpStatusCode.CommandOK) 
      Console.WriteLine(Resp.StatusDescription); 

的問題是在通話流S = Ftp.GetRequestStream();,它引發WebException的en實例並顯示消息「遠程服務器返回錯誤:(500)語法錯誤,命令無法識別」。

有人知道爲什麼是這樣嗎?

PS。我使用ECM Alfresco與虛擬FTP服務器進行通信。

編輯:我最近發現,它不可能在Windows 7上有.NET Framework 3.5的Windows服務,所以這將是很好的解決這個問題。我需要做的事情是上傳文件到FTP - 在C#中有另一種(工作)的方式?

EDIT2:以前編輯是不是真的,我有些困惑......

+0

我感興趣的是閱讀的文本,或學習,無論你似乎已經這樣做了,約Windows 7機器上.NET3.5服務的殘疾。鏈接?書名? – 2011-01-05 16:41:30

+2

啊 - 我簡直是愚蠢的。我試圖安裝該服務,並在安裝時遇到錯誤。今天我再試一次,一切正常......我想我一定是第一次錯誤地配置了我的項目... – Trakhan 2011-01-07 09:14:00

回答

4

我發現這裏http://bytes.com/topic/net/answers/792209-problems-ftpwebrequest-getrequeststream-c

類似的職位與該人工作的決議。

試試這個:

變化

reqFTP.UsePassive = true; 

reqFTP.UsePassive = false; 

UsePassive: Specifies whether to use either active or passive mode. Earlier, active FTP worked fine with all clients, but now, as most of the random ports are blocked by a firewall, the active mode may fail. The passive FTP is helpful in this case. But still, it causes issues at the server. The higher ports requested by client on server may also be blocked by a firewall. But, because FTP servers will need to make their servers accessible to the greatest number of clients, they will almost certainly need to support passive FTP. Passive mode is considered safe because it ensures all data flow initiation comes from inside (client) the network rather than from the outside (server).

而且,在每 http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.usepassive.aspx

MSDN文檔
+2

我找到了同樣的話題,但不幸的是,這並沒有爲我工作。 – Trakhan 2011-01-05 15:48:14

相關問題