FTP客戶端範例程式碼(C#原始碼):連接到DriveHQ的FTP伺服器許多使用者詢問如何在自己的軟體專案中連接到 DriveHQ 的 FTP 伺服器。本文首先提供一些關於 FTP 的基本信息,然後提供一些 C# 原始碼。
DriveHQ 提供 FTP 伺服器託管服務。我們支援標準的 FTP 和 FTPS 協定。我們 FTP 服務的主要優勢之一是:它與我們廣泛的雲端 IT 服務無縫整合。使用者可以使用 Web 瀏覽器存取他們的 FTP 檔案。 映射磁碟機(WebDAV) 或使用 DriveHQ 文件管理器客戶端軟體。他們還可以使用我們基於 Web 的群組管理工具來建立和管理 FTP 使用者(子使用者)。
普通的FTP協定由於傳輸資料時未加密,因此被認為是不安全的。為了獲得更強的SSL安全性,可以使用FTPS(基於SSL/TLS的FTP)。有兩種方法可以啟用SSL安全機制:
隱式: FTP客戶端直接連接到特殊的FTPS連接埠號碼(預設連接埠號碼為990);
顯式(FTPES): FTP 用戶端連接到常規 FTP 連接埠號碼 21,然後發出 STARTTLS 指令來升級安全性。
DriveHQ的FTP伺服器同時支援FTPS和FTPES。大多數FTP客戶端應用程式也支援FTPS和FTPES,例如FileZilla、CuteFTP、WsFTP、SmartFTP、WinSCP和Fetch等。
還有一個名為 SFTP 的安全文件傳輸協議,它經常與“安全 FTP”混淆;實際上,SFTP 指的是“SSH 文件傳輸協議”,這是一種完全不同的文件傳輸協議。從安全性的角度來看,SSL/TLS 和 SSH 基本上相同。顯然,SSL/TLS 比 SSH 更受歡迎。 DriveHQ 的 FTP 伺服器不支援 SFTP,因為我們認為目前沒有很強的 SFTP 需求。
主動模式和被動模式 FTP:
FTP 連線可能包含兩個 TCP 連線(通道):命令通道和資料通道。命令通道用於發送 FTP 命令;資料通道用於傳輸大量資料。主動模式 FTP 需要 FTP 伺服器回連 FTP 用戶端以建立資料連線。這種方式通常被認為不安全,並會被許多防火牆/路由器阻止。被動模式 FTP 需要 FTP 用戶端連接到 FTP 伺服器的不同連接埠號碼。這種方式被認為安全,很少被阻止。 DriveHQ 的 FTP 伺服器同時支援主動模式和被動模式 FTP。但是,如果您的路由器/防火牆或安全軟體封鎖了主動模式 FTP,則該功能將無法正常運作。值得注意的是,大多數新型路由器都支援主動模式 FTP。
請注意:微軟的命令列FTP客戶端不支援被動FTP。幾乎所有其他FTP用戶端程式都同時支援主動FTP和被動FTP。
FTP 和 ProFTP
DriveHQ 擁有兩個 FTP 站點,一個是 ftp.drivehq.com,所有用戶均可訪問;另一個是 proftp.drivehq.com,僅限付費會員(包括付費群組帳戶的子用戶)使用。由於 proftp.drivehq.com 僅供付費會員使用,因此不支援匿名 FTP。有關我們 FTP 伺服器託管服務的更多信息,請訪問[此處插入連結]:
http://www.drivehq.com/servicemanual.aspx#_Toc265186728
FTP客戶端原始碼範例
以下 C# 原始碼按原樣提供,不提供任何支援。您可以隨意將其用於您自己的軟體或重新分發。範例程式碼示範如何連接到我們的 FTP 伺服器並執行 FTP 命令,例如 LIST、GET 和 PUT;您還可以了解如何將其設定為使用被動 FTP 和 SSL。
使用 System;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Net;
使用 System.IO;
使用 System.Security.Cryptography.X509Certificates;
/// <摘要>
/// 版權所有 (c) 2003-2016,Drive Headquarters, Inc.
///
/// FTP 用戶端範例程式碼顯示如何使用 C# 連線到 DriveHQ 的 FTP 伺服器。
/// 您可以免費使用或重新分發此程式碼。程式碼按原樣提供,不提供任何修改
/// 不提供任何支援或擔保。 DriveHQ 對使用此程式碼不承擔任何責任。
///
命名空間 FTPClientSample
{
課程項目
{
static string ftpServer = "ftp.drivehq.com"; // 如果您有付費帳戶,也可以使用 proftp.drivehq.com。
static string username = "YOUR_USERNAME"; // 您的 DriveHQ 使用者名稱
static string password = "YOUR_PASSWORD"; // 您的 DriveHQ 密碼
static void Main(string[] args)
{
// 列出 FTP 伺服器上的一個資料夾
//列表(”/”);
// 從 FTP 伺服器下載文件
//DownloadFile("ServerFileName.jpg", @"c:\temp\ClientFileName.jpg");
// 將檔案上傳到 FTP 伺服器
UploadFile(@"c:\temp\ClientFileName.jpg", "/temp/ServerFileName.jpg");
}
/// <摘要>
/// 將檔案上傳到FTP伺服器。
///
/// FTP 伺服器上的路徑,例如 /temp/filename
///
public static bool UploadFile(string srcFilePath, string destFilePath = null)
{
如果 (String.IsNullOrWhiteSpace(srcFilePath))
throw new ArgumentNullException("來源檔案路徑。");
如果 (String.IsNullOrWhiteSpace(destFilePath))
destFilePath = Path.GetFileName(srcFilePath);
Uri serverUri = GetUri(destFilePath);
// serverUri 應以 ftp:// 協定開頭。
如果 (serverUri.Scheme != Uri.UriSchemeFtp)
返回 false;
FtpWebRequest request = CreateFtpRequest(serverUri, WebRequestMethods.Ftp.UploadFile);
// 將檔案內容讀取到位元組數組中
FileStream sourceStream = new FileStream(srcFilePath, FileMode.Open, FileAccess.Read);
byte[] fileContent = new byte[sourceStream.Length];
sourceStream.Read(fileContent, 0, fileContent.Length);
sourceStream.Close();
// 將檔案內容傳送到 FTP 伺服器
request.ContentLength = fileContent.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContent, 0, fileContent.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("回應狀態:{0} - {1}", response.StatusCode, response.StatusDescription);
傳回 true;
}
/// <摘要>
/// 將 FTP 伺服器上的 srcFilePath 檔案下載到 destFilePath 資料夾。 (如果 destFilePath 為空,則儲存到目前資料夾)
///
///
public static bool DownloadFile(string srcFilePath, string destFilePath = null)
{
如果 (String.IsNullOrWhiteSpace(srcFilePath))
throw new ArgumentNullException("來源檔案路徑。");
如果 (String.IsNullOrWhiteSpace(destFilePath))
destFilePath = Path.GetFileName(srcFilePath);
Uri serverUri = GetUri(srcFilePath);
//// serverUri 應以 ftp:// 協定開頭。
如果 (serverUri.Scheme != Uri.UriSchemeFtp)
返回 false;
FtpWebRequest request = CreateFtpRequest(serverUri, WebRequestMethods.Ftp.DownloadFile);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
FileStream fileStream = new FileStream(destFilePath, FileMode.OpenOrCreate, FileAccess.Write);
byte[] buffer = new byte[32 * 1024];
while(true) {
int bytesRead = responseStream.Read(buffer, 0, buffer.Length);
如果 (bytesRead == 0)
休息;
fileStream.Write(buffer, 0, bytesRead);
}
fileStream.Close();
Console.WriteLine("回應狀態:{0} - {1}", response.StatusCode, response.StatusDescription);
傳回 true;
}
/// <摘要>
/// 列出FTP資料夾的內容
///
///
public static string List(string path)
{
Uri serverUri = GetUri(path);
//// serverUri 應以 ftp:// 協定開頭。
如果 (serverUri.Scheme != Uri.UriSchemeFtp)
返回 ””;
FtpWebRequest request = CreateFtpRequest(serverUri, WebRequestMethods.Ftp.ListDirectory);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
string result = sr.ReadToEnd();
Console.WriteLine("回應狀態:{0} - {1}", response.StatusCode, response.StatusDescription + "\r\n" + result);
返回結果;
}
private static FtpWebRequest CreateFtpRequest(Uri serverUri, string method)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
request.EnableSsl = true;
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = true;
request.Credentials = new NetworkCredential(username, password);
請求.方法 = 方法;
退貨要求;
}
private static Uri GetUri(string remoteFilePath)
{
Uri ftpServerUri = new Uri("ftp://" + ftpServer);
返回新的 Uri(ftpServerUri, remoteFilePath);
}
}
}