C# FTPS freeze/timeout 连接到端口 990 时
C# FTPS freeze/timeout when connecting to port 990
我正在尝试将文件上传到 FTPS 文件共享。但我无法让代码工作。当下面的代码启动时,程序只是挂起,没有任何错误。最终会出现超时错误,提示系统错误。我尝试了很多东西,还有图书馆的等等。有没有人有过 FTPS 上传的经验?此代码适用于我试过的正常 FTP。
var ftpServerIP = "Ftp.company1.company2.nl:990";
var ftpUserID = "Username";
var ftpPassword = "Password";
FileInfo fileInf = new FileInfo(@"D:\testfile.txt");
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
FtpWebRequest reqFTP;
// Create FtpWebRequest object from the Uri provided
reqFTP =
(FtpWebRequest)FtpWebRequest.Create(
new Uri("ftp://" + ftpServerIP + "/IDEE_MOX/" + fileInf.Name));
reqFTP.EnableSsl = true;
// Provide the WebPermission Credintials
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInf.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = fileInf.OpenRead();
try
{
Stream strm = reqFTP.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
fs.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Upload Error");
}
有谁知道这段代码有什么问题以及它为什么冻结?我有点卡在这里了。
.NET 框架 (FtpWebRequest
) 不支持隐式 TLS/SSL。仅显式,参见:
Does .NET FtpWebRequest Support both Implicit (FTPS) and explicit (FTPES)?
因此,您无法连接到端口 990(隐式 TLS/SSL)。
虽然您的 FTP 服务器很可能也支持显式 TLS/SSL,但请使用它。您已经设置了 EnableSsl
(什么是明确的 TLS/SSL)。所以只要连接到默认端口21就大功告成了:
var ftpServerIP = "Ftp.company1.company2.nl";
(无需指定显式端口,因为 21 是默认端口)
在极少数情况下,您的服务器不支持显式 TLS/SSL,您需要使用第 3 方 FTP 客户端库。上面链接的问题中提到了一些。 我的 WinSCP .NET assembly supports the implicit FTPS too. As you have already made the connection working with WinSCP GUI, using the implicit TLS/SSL, you can have a code template generated.
我正在尝试将文件上传到 FTPS 文件共享。但我无法让代码工作。当下面的代码启动时,程序只是挂起,没有任何错误。最终会出现超时错误,提示系统错误。我尝试了很多东西,还有图书馆的等等。有没有人有过 FTPS 上传的经验?此代码适用于我试过的正常 FTP。
var ftpServerIP = "Ftp.company1.company2.nl:990";
var ftpUserID = "Username";
var ftpPassword = "Password";
FileInfo fileInf = new FileInfo(@"D:\testfile.txt");
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
FtpWebRequest reqFTP;
// Create FtpWebRequest object from the Uri provided
reqFTP =
(FtpWebRequest)FtpWebRequest.Create(
new Uri("ftp://" + ftpServerIP + "/IDEE_MOX/" + fileInf.Name));
reqFTP.EnableSsl = true;
// Provide the WebPermission Credintials
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInf.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = fileInf.OpenRead();
try
{
Stream strm = reqFTP.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
fs.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Upload Error");
}
有谁知道这段代码有什么问题以及它为什么冻结?我有点卡在这里了。
.NET 框架 (FtpWebRequest
) 不支持隐式 TLS/SSL。仅显式,参见:
Does .NET FtpWebRequest Support both Implicit (FTPS) and explicit (FTPES)?
因此,您无法连接到端口 990(隐式 TLS/SSL)。
虽然您的 FTP 服务器很可能也支持显式 TLS/SSL,但请使用它。您已经设置了 EnableSsl
(什么是明确的 TLS/SSL)。所以只要连接到默认端口21就大功告成了:
var ftpServerIP = "Ftp.company1.company2.nl";
(无需指定显式端口,因为 21 是默认端口)
在极少数情况下,您的服务器不支持显式 TLS/SSL,您需要使用第 3 方 FTP 客户端库。上面链接的问题中提到了一些。 我的 WinSCP .NET assembly supports the implicit FTPS too. As you have already made the connection working with WinSCP GUI, using the implicit TLS/SSL, you can have a code template generated.