【发布时间】:2017-01-16 13:30:22
【问题描述】:
我正在尝试将文件上传到 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");
}
有谁知道这段代码有什么问题以及为什么会冻结?我有点卡在这里。
【问题讨论】:
-
冻结在哪一行?
-
@BugFinder 它运行 Stream strm = reqFTP.GetRequestStream();然后它就冻结了
-
您正在使用端口 990。服务器是否在此端口上侦听 ftp?你想使用 FTP 还是 FTPS?端口 990 用于 ftp TSL/SSL。由于它是一个众所周知的端口号(under1028),它可能会被防火墙或病毒检查程序阻止。端口 990 用于控制,数据在不同的端口号上发送。您应该使用像 wireshark 或 fiddler 这样的嗅探器来帮助开发代码。
-
@jdweng 是的,服务器是端口 990 FTPS (SSL DIRECT)。我可以毫无问题地使用 filezilla 连接到服务器。所以你想说的是我的电脑上有什么东西(防火墙或病毒检查器等)阻止了这个端口?就这样冻死了?
-
我认为数据位于不同的端口号上,而您只使用一个端口。使用带有 filezilla 的嗅探器并将结果与您的应用程序进行比较
标签: c# .net ftp ftpwebrequest ftps