无法连接到System.Net.FtpWebRequest.GetRequestStream()上的远程服务器

时间:2015-01-02 21:02:30

标签: c# ftp ftpwebrequest

我可以在Debug中运行VS2013时FTP到服务器并上传文件,但是当我编译为Release时x86我在调用System.Net.FtpWebRequest.GetRequestStream()的行中收到错误“无法连接到远程服务器”。 我可以使用WinSCP访问FTP并上传文件。我运行WireShark并且从我的代码的Release版本看不到任何数据包,但是当我在VS2013中从Debug模式执行时看到了所有相关的数据包。我没有防火墙,这真让我难过。任何帮助将不胜感激。 - 查克

    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() 

1 个答案:

答案 0 :(得分:0)

一个很好的建议是使用网络跟踪功能(感谢user957902!) 然而,由系统管理员管理的Bitdefender'端点安全'是罪魁祸首。我相信这是某种防火墙运行的防火墙阻止来自某些应用程序(我编译的东西)的FTP流量,并允许其他人的传递(VS2013)。在解决此类问题时,请始终查看您的安全软件并询问您的系统管理员,特别是如果流量在某些情况下有效而在其他情况下无效 - 如果对您没有意义,请尝试将此类实用程序排除在外

的问候, 卡盘