請第一次看到我的窗戶形式代碼:如何在使用的FtpWebRequest被動模式及解決PASV錯誤的.Net 3.5定義端口範圍內碼
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我得到這個錯誤:
「服務器響應PASV命令返回的地址 比來作出該FTP連接地址不同。」
下面的原因,我不想使用active mode
:
如你所知被動模式更是主動模式 連接...
當我使用主動模式下.net 3.5和 打開代理軟件我得到了以下錯誤:
「底層連接已關閉:服務器承諾違反協議 」。
但.NET 4具有與代理軟件和被動模式沒有問題,我不能切換到.NET 4,因爲我的用戶...
所以我怎樣才能解決被動在.NET 3.5模式錯誤?
在每個線程堆棧人說只使用:
_FtpWebRequest.UsePassive = false;
,這是不是我的答案!
注:防火牆在服務器和客戶端是關閉
的另一個問題是:
是有可能通過碼中以定義端口範圍的pssive模式?
我問這個問題在這個線程,因爲我認爲這樣做,我們可以修復PASV錯誤,並幫助被動模式做它的工作速度更快...
編輯:
我發現下面&線程我想我已經回覆#2的情況下,
ftp-problem
我在我的服務器兩個網絡適配器和每個人的IP裏面的服務器就像192.168.5。 & 192.168.5。
但我的兩個公共IP地址是不同的。
所以我怎麼能通過改變我的代碼或我的Windows服務器2008-R2 VPS中的東西來解決這個錯誤,以及爲什麼這個錯誤只出現在.net 3.5和.net 4中,我們沒有它?
我可以完全訪問我的服務器,並可以更改每一件必要的事情。
在此先感謝