3

最初に私の 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

            }
        }

Windows Server 2008 r2 サーバーにデータをアップロードするためにUploadFileメソッドを使用しています。.net 4 のこれらのコードは完璧に機能し、私の問題は.net 3.5についてです。.net 3.5 では、次のエラーが発生しました:

「サーバーは、PASV コマンドに応答して、FTP 接続が確立されたアドレスとは異なるアドレスを返しました。」

以下の理由により、使用したくありませんactive mode

  1. ご存じのように、パッシブ モードはアクティブ モードよりも接続性に優れています...

  2. .net 3.5 でアクティブ モードを使用していて、プロキシ ソフトウェアをオンにすると、以下のエラーが発生しました。

「基になる接続が閉じられました: サーバーがプロトコル違反を犯しました。」

しかし、.net 4にはそのプロキシソフトウェアとパッシブモードに 問題はなく、ユーザーのために.net 4に切り替えることができません...どうすれば.net 3.5
のパッシブモードエラーを修正できますか? スタック内のすべてのスレッドで、人々は単に使用すると言います:

    _FtpWebRequest.UsePassive = false;    

これは私の答えではありません!

注:サーバーとクライアントの両方のファイアウォールはオフになっています


他の質問は次のとおりです。

pssive モードのポート範囲をコードで定義することはできますか? このスレッドでこの質問をしたのは、そうすることでPASVエラーを修正し、パッシブモードがより速く仕事をするのを助けることができると思ったからです...



編集:
以下のスレッドを見つけました & 私は返信 #2、
ftp-problem
に状況があると思い ます サーバーに 2 つのネットワーク アダプターがあり、サーバー内のそれぞれの IP は 192.168.5.?? のようです。& 192.168.5.??
しかし、私の 2 つのパブリック IP アドレスは異なります。
コードまたはWindowsサーバー2008-r2 VPSで何かを変更してそのエラーを修正するにはどうすればよいですか?また、そのエラーが.net 3.5にのみ表示され、.net 4には表示されないのはなぜですか?
私は自分のサーバーに完全にアクセスでき、必要なものをすべて変更できます。

前もって感謝します

4

1 に答える 1

3

これがあなたの答えです:問題は.net 3.5および.net 4
とは何の関係もないようです。プロキシソフトウェアエラーの場合、以下のconfiguring-ftp-firewall-settings-in-iis-7の ようにサーバー内でその問題を修正できます:ポートの変更範囲。パッシブ エラーの場合: ファイアウォールの外部 IP アドレスをパブリック IP アドレスに変更します。



画像

EDIT:
コードビハインドでポート範囲を定義することは可能ですか?

于 2014-01-10T17:59:50.067 に答える