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

标签 c# winforms ftp .net-3.5 passive-mode

请先看我的窗体代码:

        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."

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

  1. 如您所知,被动模式优于主动模式 连通性...

  2. 当我在 .net 3.5 中使用事件模式并且 打开代理软件我得到以下错误:

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

但是 .net 4 对那个代理软件 和被动模式没问题,我不能切换到 .net 4 因为我用户...
那么如何修复 .net 3.5 中的被动模式错误?
在堆栈中的每个线程中,人们都说只使用:

    _FtpWebRequest.UsePassive = false;    

这不是我的答案!

注意:服务器和客户端的防火墙都关闭


另一个问题是:

是否可以通过代码定义psessive modeport-range
我在这个线程中问了这个问题,因为我认为通过这样做我们可以修复 PASV 错误并帮助 passive-mode 更快地完成它的工作...



编辑:
我找到了下面的主题,我想我在回复 #2 中遇到了这种情况,
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 地址更改为您的公共(public) IP 地址。

img

编辑:
真的很感激其他人了解我们是否可以在代码隐藏中定义端口范围?

关于c# - 如何在 FtpWebRequest 中使用被动模式并修复 .Net 3.5 中的 PASV 错误并通过代码定义端口范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21032577/

相关文章:

c# - 使用按钮 C# 控制从一个表单到另一个表单的对象属性

javascript - 无法使用 npm 中的 "ftp"包连接到 ftp 服务器

c# - 如何从 javascript 设置 C# 变量值

c# - 异步任务取消 c# xamarin

c# - 有趣的错误 : new form has black line across it, 我该如何摆脱它

c# - 在属性树上使用 IWindowsFormsEditorService 进行下拉列表不会关闭

java - java中的FTP上传目录树

ios - 是否可以在 iOS 中通过 FTP 写入远程文件?

c# - ILGenerator : Load created method

c# - C#中抽象类的构造函数