我想用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接口打印如下:
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。我必須安裝服務器證書還是什麼?或者是代碼錯誤?