c#-如何在FtpWebRequest中使用被动模式并修复.Net 3.5中的PASV错误并通过代码定义端口范围

请先查看我的Windows表单代码:

        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Linq;
        using System.Text;
        using System.Windows.Forms;

        namespace my_prog
        {
            public partial class Form1 : Form
            {
                public Form1()
                {
                    InitializeComponent();
                }
                string ftp_username = "goodzilla_user";
                string ftp_password = "goodzilla_pass";
                string ftp_remote_host = @"ftp://11.11.111.11";

                private void Form1_Load(object sender, EventArgs e)
                {
                    UploadFile("d:\\test.txt", ftp_remote_host + @"/test.txt", ftp_username, ftp_password);
                }

                #region UploadFile Method

                /// <summary>
                /// Methods to upload file to FTP Server
                /// </summary>
                /// <param name="_FileName">local source file name</param>
                /// <param name="_UploadPath">Upload FTP path including Host name</param>
                /// <param name="_FTPUser">FTP login username</param>
                /// <param name="_FTPPass">FTP login password</param>
                /// 
                public void UploadFile(string _FileName, string _UploadPath, string _FTPUser, string _FTPPass)
                {
                    System.IO.FileInfo _FileInfo = new System.IO.FileInfo(_FileName);

                    // Create FtpWebRequest object from the Uri provided
                    System.Net.FtpWebRequest _FtpWebRequest = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(new Uri(_UploadPath));

                    // Provide the WebPermission Credintials
                    _FtpWebRequest.Credentials = new System.Net.NetworkCredential(_FTPUser, _FTPPass);

                    // By default KeepAlive is true, where the control connection is not closed
                    // after a command is executed.
                    _FtpWebRequest.KeepAlive = false;

                    // set timeout for 20 seconds
                    _FtpWebRequest.Timeout = 20000;

                    // Specify the command to be executed.
                    _FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile;

                    // Specify the data transfer type.
                    _FtpWebRequest.UseBinary = true;

                    // Notify the server about the size of the uploaded file
                    _FtpWebRequest.ContentLength = _FileInfo.Length;

                    // The buffer size is set to 2kb
                    int buffLength = 2048;
                    byte[] buff = new byte[buffLength];

                    // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
                    System.IO.FileStream _FileStream = _FileInfo.OpenRead();

                    try
                    {
                        // Stream to which the file to be upload is written
                        System.IO.Stream _Stream = _FtpWebRequest.GetRequestStream();

                        // Read from the file stream 2kb at a time
                        int contentLen = _FileStream.Read(buff, 0, buffLength);

                        // Till Stream content ends
                        while (contentLen != 0)
                        {
                            // Write Content from the file stream to the FTP Upload Stream
                            _Stream.Write(buff, 0, contentLen);
                            contentLen = _FileStream.Read(buff, 0, buffLength);
                        }

                        // Close the file stream and the Request Stream
                        _Stream.Close();
                        _Stream.Dispose();
                        _FileStream.Close();
                        _FileStream.Dispose();

                        MessageBox.Show("Done");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Upload Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }

                #endregion

            }
        }

我正在使用UploadFile方法将数据上传到Windows Server 2008 R2服务器.
.net 4中的这些代码工作完美,我的问题是关于.net 3.5.
在.net 3.5中,我收到此错误:

“The server returned an address in response to the PASV command that
is different than the address to which the FTP connection was made.”

由于以下原因,我不想使用主动模式:

>如您所知,被动模式优于主动模式
连接…
>当我在.net 3.5和
打开代理软件,我得到以下错误:

“The underlying connection was closed: The server committed a protocol
violation.”

但是.net 4的代理软件和被动模式没有问题,由于我的用户,我无法切换到.net 4.
所以我该如何修复.net 3.5中的被动模式错误?
人们在栈中的每个线程中都说:

    _FtpWebRequest.UsePassive = false;    

这不是我的答案!

注意:服务器和客户端的防火墙均已关闭
 
另一个问题是:

是否可以通过代码定义伪模式的端口范围?
我在这个线程中问了这个问题,因为我认为这样做可以修复PASV错误并帮助被动模式更快地完成工作.

编辑:
我在&下找到了线程我想我有第二回信的情况,
ftp-problem
我在服务器中有两个网络适配器,服务器内部每个IP都像192.168.5. & 192.168.5.
但是我的两个公共IP地址不同.
那么,如何通过更改代码或Windows Server 2008-r2 VPS中的某些内容来解决该错误,为什么该错误仅出现在.net 3.5和.net 4中却没有呢?
我拥有对服务器的完全访问权限,并且可以更改所有必要的内容.

提前致谢

解决方法:

这是你的答案:
看来问题与.net 3.5.net 4没有关系
您可以在服务器内部修复该问题,如下所示
configuring-ftp-firewall-settings-in-iis-7
对于代理软件错误:更改端口范围.
对于被动错误:将防火墙的外部IP地址更改为您的公共IP地址.

编辑:
其他人了解我们是否真的很感激,是否有可能在后面的代码中定义端口范围?

上一篇:c#-FormClosing之后的计时器刻度?


下一篇:C#-修剪switch语句