无法连接到位于 System.Net.FtpWebRequest.GetRequestStream() 的远程服务器
Unable to connect to the remote server at System.Net.FtpWebRequest.GetRequestStream()
在 Debug 中运行 VS2013 时,我能够 FTP 到服务器并上传文件,但是当我编译为 Release 时 | x86 我在调用 System.Net.FtpWebRequest.GetRequestStream() 的行收到错误 "Unable to connect to the remote server"。
我可以使用 WinSCP 访问 FTP 并上传文件。我已经运行 WireShark 并且在我的代码的发布版本中没有看到任何数据包,但是当我在 VS2013 中从调试模式执行时看到了所有相关的数据包。我没有防火墙,这真的让我很困惑。任何帮助将不胜感激。 - 查克
public bool UploadToFtp(string sFtpServer, string sFtpUserName, string sFtpPassword, string sSourceLocationPath, string sFtpDestinationPath, StringBuilder sbLogMessage)
{
bool bUpload;
try
{
var oFile = new FileInfo(sSourceLocationPath);
var oUri = new Uri(sFtpServer + sFtpDestinationPath + oFile.Name);
var oFtp = (FtpWebRequest)WebRequest.Create(oUri);
oFtp.Credentials = new NetworkCredential(sFtpUserName, sFtpPassword);
oFtp.KeepAlive = false;
oFtp.UseBinary = true;
oFtp.UsePassive = true;
oFtp.Timeout = 10000000;
oFtp.ReadWriteTimeout = 10000000;
oFtp.Method = WebRequestMethods.Ftp.UploadFile;
oFtp.ContentLength = oFile.Length;
var arrContent = File.ReadAllBytes(oFile.FullName);
using (var oFileStream = oFile.OpenRead())
{
using (var oStream = oFtp.GetRequestStream())
{
int iReadBytes;
do
{
iReadBytes = oFileStream.Read(arrContent, 0, int.Parse(oFile.Length.ToString(CultureInfo.InvariantCulture)));
oStream.Write(arrContent, 0, iReadBytes);
} while (iReadBytes != 0);
bUpload = true;
oStream.Flush();
oStream.Close();
oFileStream.Close();
}
}
File.Delete(sSourceLocationPath + oFile.Name);
}
catch (Exception ex)
{
LogMessage(ex.Message);
bUpload = false;
}
return bUpload;
}
***** 编辑 *****
我按照建议创建了一个网络跟踪日志并得到了以下输出(注意:我已经更改了此清单中的实际 IP # 和文件名):
System.Net Verbose: 0 : [27252] WebRequest::Create(ftp://1.2.3.4//file.zip)
System.Net Information: 0 : [27252] FtpWebRequest#28316044::.ctor(ftp://1.2.3.4//file.zip)
System.Net Verbose: 0 : [27252] Exiting WebRequest::Create() -> FtpWebRequest#28316044
System.Net Verbose: 0 : [27252] FtpWebRequest#28316044::GetRequestStream()
System.Net Information: 0 : [27252] FtpWebRequest#28316044::GetRequestStream(Method=STOR.)
System.Net Information: 0 : [27252] Current OS installation type is 'Client'.
System.Net Verbose: 0 : [27252] ServicePoint#39974954::ServicePoint(1.2.3.4:21)
System.Net.Sockets Verbose: 0 : [27252] Socket#24230272::Socket(AddressFamily#2)
System.Net.Sockets Verbose: 0 : [27252] Exiting Socket#24230272::Socket()
System.Net.Sockets Verbose: 0 : [27252] Socket#16745860::Socket(AddressFamily#23)
System.Net.Sockets Verbose: 0 : [27252] Exiting Socket#16745860::Socket()
System.Net.Sockets Verbose: 0 : [27252] DNS::TryInternalResolve(1.2.3.4)
System.Net.Sockets Verbose: 0 : [27252] Socket#24230272::Connect(1.2.3.4:21#1294145406)
System.Net.Sockets Error: 0 : [27252] Socket#24230272::UpdateStatusAfterSocketError() - TimedOut
System.Net.Sockets Error: 0 : [27252] Exception in Socket#24230272::Connect - A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 1.2.3.4:21.
System.Net Error: 0 : [27252] Exception in FtpWebRequest#28316044::GetRequestStream - Unable to connect to the remote server.
at System.Net.FtpWebRequest.GetRequestStream()
System.Net Verbose: 0 : [27252] Exiting FtpWebRequest#28316044::GetRequestStream()
一个很好的建议是使用网络跟踪功能(感谢 user957902!)
然而,罪魁祸首是由系统管理员管理的 Bitdefender 'endpoint security'。我相信这是某种防火墙 运行 在隐身模式下阻止 FTP 来自某些应用程序(我编译的东西)的流量并允许其他应用程序通过(VS2013)。在解决此类问题时,请始终查看您的安全软件并询问您的系统管理员,特别是如果流量在某些情况下有效但在其他情况下无效 - 如果它对您没有意义,请尝试将此类实用程序排除在外.
此致,
查克
在 Debug 中运行 VS2013 时,我能够 FTP 到服务器并上传文件,但是当我编译为 Release 时 | x86 我在调用 System.Net.FtpWebRequest.GetRequestStream() 的行收到错误 "Unable to connect to the remote server"。 我可以使用 WinSCP 访问 FTP 并上传文件。我已经运行 WireShark 并且在我的代码的发布版本中没有看到任何数据包,但是当我在 VS2013 中从调试模式执行时看到了所有相关的数据包。我没有防火墙,这真的让我很困惑。任何帮助将不胜感激。 - 查克
public bool UploadToFtp(string sFtpServer, string sFtpUserName, string sFtpPassword, string sSourceLocationPath, string sFtpDestinationPath, StringBuilder sbLogMessage)
{
bool bUpload;
try
{
var oFile = new FileInfo(sSourceLocationPath);
var oUri = new Uri(sFtpServer + sFtpDestinationPath + oFile.Name);
var oFtp = (FtpWebRequest)WebRequest.Create(oUri);
oFtp.Credentials = new NetworkCredential(sFtpUserName, sFtpPassword);
oFtp.KeepAlive = false;
oFtp.UseBinary = true;
oFtp.UsePassive = true;
oFtp.Timeout = 10000000;
oFtp.ReadWriteTimeout = 10000000;
oFtp.Method = WebRequestMethods.Ftp.UploadFile;
oFtp.ContentLength = oFile.Length;
var arrContent = File.ReadAllBytes(oFile.FullName);
using (var oFileStream = oFile.OpenRead())
{
using (var oStream = oFtp.GetRequestStream())
{
int iReadBytes;
do
{
iReadBytes = oFileStream.Read(arrContent, 0, int.Parse(oFile.Length.ToString(CultureInfo.InvariantCulture)));
oStream.Write(arrContent, 0, iReadBytes);
} while (iReadBytes != 0);
bUpload = true;
oStream.Flush();
oStream.Close();
oFileStream.Close();
}
}
File.Delete(sSourceLocationPath + oFile.Name);
}
catch (Exception ex)
{
LogMessage(ex.Message);
bUpload = false;
}
return bUpload;
}
***** 编辑 ***** 我按照建议创建了一个网络跟踪日志并得到了以下输出(注意:我已经更改了此清单中的实际 IP # 和文件名):
System.Net Verbose: 0 : [27252] WebRequest::Create(ftp://1.2.3.4//file.zip)
System.Net Information: 0 : [27252] FtpWebRequest#28316044::.ctor(ftp://1.2.3.4//file.zip)
System.Net Verbose: 0 : [27252] Exiting WebRequest::Create() -> FtpWebRequest#28316044
System.Net Verbose: 0 : [27252] FtpWebRequest#28316044::GetRequestStream()
System.Net Information: 0 : [27252] FtpWebRequest#28316044::GetRequestStream(Method=STOR.)
System.Net Information: 0 : [27252] Current OS installation type is 'Client'.
System.Net Verbose: 0 : [27252] ServicePoint#39974954::ServicePoint(1.2.3.4:21)
System.Net.Sockets Verbose: 0 : [27252] Socket#24230272::Socket(AddressFamily#2)
System.Net.Sockets Verbose: 0 : [27252] Exiting Socket#24230272::Socket()
System.Net.Sockets Verbose: 0 : [27252] Socket#16745860::Socket(AddressFamily#23)
System.Net.Sockets Verbose: 0 : [27252] Exiting Socket#16745860::Socket()
System.Net.Sockets Verbose: 0 : [27252] DNS::TryInternalResolve(1.2.3.4)
System.Net.Sockets Verbose: 0 : [27252] Socket#24230272::Connect(1.2.3.4:21#1294145406)
System.Net.Sockets Error: 0 : [27252] Socket#24230272::UpdateStatusAfterSocketError() - TimedOut
System.Net.Sockets Error: 0 : [27252] Exception in Socket#24230272::Connect - A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 1.2.3.4:21.
System.Net Error: 0 : [27252] Exception in FtpWebRequest#28316044::GetRequestStream - Unable to connect to the remote server.
at System.Net.FtpWebRequest.GetRequestStream()
System.Net Verbose: 0 : [27252] Exiting FtpWebRequest#28316044::GetRequestStream()
一个很好的建议是使用网络跟踪功能(感谢 user957902!) 然而,罪魁祸首是由系统管理员管理的 Bitdefender 'endpoint security'。我相信这是某种防火墙 运行 在隐身模式下阻止 FTP 来自某些应用程序(我编译的东西)的流量并允许其他应用程序通过(VS2013)。在解决此类问题时,请始终查看您的安全软件并询问您的系统管理员,特别是如果流量在某些情况下有效但在其他情况下无效 - 如果它对您没有意义,请尝试将此类实用程序排除在外.
此致, 查克