C# ftp ssl download

  • 7425
  • 0
  • 2018-06-29

C# ftp ssl download

以下敘述嘗試歷程:

Here's what I try:

 

我有個網站放在一般Server服務商上,想用ftp去取得網站下的檔案

I have a website on a server, and I want to get the file under the website through FTP,

 

我先以FileZilla輸入要FTP的網址、帳密,確實成功登入取得伺服器列表和檔案後,

再用同樣的網址帳密使用C#實驗ftp看看,同時我想要使用ssl

First I use FileZilla with ftp url, username, password successfully get the file,

then I try to use same information to get the file using C# FTP,

 

 

單純FTP,未使用SSL加密,查到MSDN文件程式範例為:

I found MSDN C# code Example using FTP without SSL  here:

https://msdn.microsoft.com/zh-tw/library/ms229711(v=vs.110).aspx

// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
request.Method = WebRequestMethods.Ftp.DownloadFile;

// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");

FtpWebResponse response = (FtpWebResponse)request.GetResponse();

Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
Console.WriteLine(reader.ReadToEnd());

Console.WriteLine("Download Complete, status {0}", response.StatusDescription);

reader.Close();
response.Close(); 

 

要使用ssl加密,網路上查到可將FtpWebRequest.EnableSsl設為true

And I found that you can set FtpWebRequest.EnableSsl = true to enable SSL

https://msdn.microsoft.com/zh-tw/library/system.net.ftpwebrequest.enablessl(v=vs.110).aspx

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
request.Method = WebRequestMethods.Ftp.DownloadFile;
//增加這行
request.EnableSsl = true;

但嘗試後發現,這在驗證過程會發生錯誤,錯誤訊息為

but I got this error when GetResponse:

System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.

 

後來網路上查到這篇文章解法:

Later I found the solution in this article:

http://www.wittersworld.com/2008/12/12/ftps-over-tlsssl-using-c/

參考他的做法,修改為以下程式碼後,就可正確取得我要取得的網站檔案:

the code modified below works:

public class WebRequestGetExample
{
	public void FtpDownload ()
	{
		//增加這行 add this line
		ServicePointManager.ServerCertificateValidationCallback = AcceptAllCertificatePolicy;

		FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
		request.Method = WebRequestMethods.Ftp.DownloadFile;

		//增加這行 add this line
		request.EnableSsl = true;


		request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");

		FtpWebResponse response = (FtpWebResponse)request.GetResponse();

		Stream responseStream = response.GetResponseStream();
		StreamReader reader = new StreamReader(responseStream);
		Console.WriteLine(reader.ReadToEnd());

		Console.WriteLine("Download Complete, status {0}", response.StatusDescription);

		reader.Close();
		response.Close();  
	}


	//增加這個方法 add this method
	public bool AcceptAllCertificatePolicy(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
	{
		return true;
	}

}