2017-07-14 45 views
0

我想用C#做一個到FileZilla服務器的FTPS連接,但C#應用程序從不登錄到服務器,服務器關閉連接,並收到以下錯誤C#:C#FTP顯式SSL請求從不登錄

The underlying connection was closed: The server committed a protocol violation. 

我的C#代碼是像這樣:

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; 
var req = (FtpWebRequest)WebRequest.Create("ftp://<my IP>:<my port>/"); 
req.UsePassive = true; 
req.UseBinary = true; 
req.EnableSsl = true; 
req.KeepAlive = true; 
req.Credentials = new NetworkCredential("Puff", "Daddy"); 
req.Method = WebRequestMethods.Ftp.ListDirectory; 
using (var response = (FtpWebResponse)req.GetResponse()) 
{ 
    //Do Something 
} 

在一個連接嘗試的FileZilla Server接口打印如下:

FileZilla Log

C#的網絡日誌中讀取,像這樣:

System.Net.Sockets Verbose: 0 : [6424] Socket#54135081::Socket(AddressFamily#23) 
System.Net.Sockets Verbose: 0 : [6424] Exiting Socket#54135081::Socket() 
System.Net.Sockets Verbose: 0 : [6424] DNS::TryInternalResolve(<FTP Server IP>) 
System.Net.Sockets Verbose: 0 : [6424] Socket#5773521::Connect(<FTP Server IP>:<FTP Port>#-1925092664) 
System.Net.Sockets Information: 0 : [6424] Socket#5773521 - Created connection from <local IP>:61169 to <Server IP>:<Server Port>. 
System.Net.Sockets Verbose: 0 : [6424] Exiting Socket#5773521::Connect() 
System.Net.Sockets Verbose: 0 : [6424] Socket#54135081::Close() 
System.Net.Sockets Verbose: 0 : [6424] Socket#54135081::Dispose() 
System.Net.Sockets Verbose: 0 : [6424] Exiting Socket#54135081::Close() 
System.Net Information: 0 : [6424] FtpControlStream#63094882 - Created connection from <Local IP>:61169 to <Server IP>:<Server Port>. 
System.Net Information: 0 : [6424] Associating FtpWebRequest#59817589 with FtpControlStream#63094882 
System.Net.Sockets Verbose: 0 : [6424] Socket#5773521::Receive() 
System.Net.Sockets Verbose: 0 : [6424] Data from Socket#5773521::Receive 
System.Net.Sockets Verbose: 0 : [6424] 00000000 :             : 
System.Net.Sockets Verbose: 0 : [6424] Exiting Socket#5773521::Receive() -> Int32#0 
System.Net.Sockets Verbose: 0 : [6424] Socket#5773521::Dispose() 
System.Net Information: 0 : [6424] FtpWebRequest#59817589::(Releasing FTP connection#63094882.) 
System.Net Error: 0 : [6424] Exception in FtpWebRequest#59817589::GetResponse - The underlying connection was closed: The server committed a protocol violation. 

我已經成功地與來自同一臺電腦與FileZilla的客戶端相同的參數的連接,所以我知道它應該工作。

我已將這些行添加到App.Config。

<system.net> 
    <settings> 
    <servicePointManager expect100Continue="false"/> 
    <httpWebRequest useUnsafeHeaderParsing="true"/> 
    </settings> 
</system.net> 

我也試過針對.NET Framework 3.5和2.0,但都有相同的結果。目前我正在運行.NET 4.6。我必須安裝服務器證書還是什麼?或者是代碼錯誤?

回答

1

我發佈這個問題後約2分鐘,答案就來了。如果其他人有這些掙扎,我會放棄它(我有時候喜歡接受某人認同我的問題的想法,但不太可能)。

在FileZilla服務器上的「FTP over TLS settings」對話框中,我配置了一個偵聽隱式FTP或TLS(默認值爲990)的端口。這是我在FZ Client FTPS連接中使用的端口。我也試圖從C#中使用這個端口,這是行不通的,因爲連接是顯式的FTPS。我不得不使用常規設置中指定的端口作爲偵聽端口(默認值爲21)。

在發佈問題之前,我實際上已經嘗試過,但忘了應用路由器的配置,因此無法正常工作,「Der」。