c# - C# FTPS FtpWebRequest 设置被动模式以使用端口范围
问题描述
我无法使用 C# 连接到特定的 FTP FtpWebRequest
。
使用 TotalCommander 或 FileZilla 我可以连接。
有关 FTP 的信息说:
FTPS – 隐式 SSL/TLS
端口 990
被动端口范围:60000–60100
另一个 FTP 运行良好。
var remoteFile = "ftp://ADDRESS:990/FILE.csv";
var localFile = Server.MapPath("~/tmp/file.csv");
int bufferSize = 2048;
if (!Directory.Exists(Path.GetDirectoryName(localFile)))
Directory.CreateDirectory(Path.GetDirectoryName(localFile));
/* Create an FTP Request */
FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(remoteFile);
/* Log in to the FTP Server with the User Name and Password Provided */
ftpRequest.Credentials = new NetworkCredential("USER", "PWD");
/* When in doubt, use these options */
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.EnableSsl = true;
// Always returns true
ServicePointManager.ServerCertificateValidationCallback = OnValidateCertificate;
/* Specify the Type of FTP Request */
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
/* Establish Return Communication with the FTP Server */
FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
/* Get the FTP Server's Response Stream */
Stream ftpStream = ftpResponse.GetResponseStream();
/* Open a File Stream to Write the Downloaded File */
FileStream localFileStream = new FileStream(localFile, FileMode.Create);
/* Buffer for the Downloaded Data */
byte[] byteBuffer = new byte[bufferSize];
int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
/* Download the File by Writing the Buffered Data Until the Transfer is Complete */
try
{
while (bytesRead > 0)
{
localFileStream.Write(byteBuffer, 0, bytesRead);
bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
/* Resource Cleanup */
localFileStream.Close();
ftpStream.Close();
ftpResponse.Close();
ftpRequest = null;
在我看来,该错误是由被动端口范围 60000–60100 引起的。但我不知道如何设置它。
解决方案
FTP 被动端口范围是服务器端配置。
您没有在客户端设置被动端口范围——FileZilla 和 Total Commander 也没有这样的配置选项。FTP 客户端使用服务器选择的端口。
您的实际问题是 .NET/FtpWebRequest
不支持隐式TLS/SSL:
.NET FtpWebRequest 是否同时支持隐式 (FTPS) 和显式 (FTPES)?
推荐阅读
- java - Vert.x 使用 OpenSslEngine 作为 HTTP 服务器
- r - 如何使用分面网格和位置闪避对绘图中的点范围进行排序和着色?
- typescript - Typescript 如何实现工厂模式
- php - 将 echo php 添加到 jQuery 窗口确认()
- makefile - 是否可以强制 GNU Make 更具体地说明实际缺少哪些依赖项?
- python - 比较数据框中的两列并使用熊猫样式突出显示值
- php - PHP XPath - 使用“评估”读取节点属性时遇到问题
- python - 如何在pyqt5中允许tab键按下事件
- java - LibGDX:从坠落的物体施加自然力
- matlab - 如何调用 M 文件到 Simulink 模型?