問(wèn)題描述
我下面的代碼在我的電腦上運(yùn)行得很好,沒(méi)有代理.但是在客戶端服務(wù)器中,他們需要向 FTP 客戶端 (FileZilla) 添加代理才能訪問(wèn) FTP.但是當(dāng)我添加代理時(shí)它說(shuō)
My below code works perfectly fine in my computer without proxy. But in client server they need to add proxy to the FTP client (FileZilla) to be able to access the FTP. But When I add proxy it says
使用代理時(shí)無(wú)法啟用 SSL.
SSL cannot be enabled when using a proxy.
FTP 代理
var proxyAddress = ConfigurationManager.AppSettings["ProxyAddress"];
WebProxy ftpProxy = null;
if (!string.IsNullOrEmpty(proxyAddress))
{
var proxyUserId = ConfigurationManager.AppSettings["ProxyUserId"];
var proxyPassword = ConfigurationManager.AppSettings["ProxyPassword"];
ftpProxy = new WebProxy
{
Address = new Uri(proxyAddress, UriKind.RelativeOrAbsolute),
Credentials = new NetworkCredential(proxyUserId, proxyPassword)
};
}
FTP 連接
var ftpRequest = (FtpWebRequest)WebRequest.Create(ftpAddress);
ftpRequest.Credentials = new NetworkCredential(
username.Normalize(),
password.Normalize()
);
ServicePointManager.ServerCertificateValidationCallback +=
(sender, cert, chain, sslPolicyErrors) => true;
ServicePointManager.Expect100Continue = false;
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
ftpRequest.EnableSsl = true;
//ftpRequest.Proxy = ftpProxy;
var response = (FtpWebResponse)ftpRequest.GetResponse();
推薦答案
.NET 框架確實(shí)不支持通過(guò)代理的 TLS/SSL 連接.
.NET framework indeed does not support TLS/SSL connections over proxy.
您必須使用第 3 方 FTP 庫(kù).
You have to use a 3rd party FTP library.
另外請(qǐng)注意,您的代碼沒(méi)有使用隱式"FTPS.它使用顯式"FTPS..NET 框架也不支持隱式 FTPS.
例如,對(duì)于 WinSCP .NET 程序集,您可以使用:
For example with WinSCP .NET assembly, you can use:
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = "example.com",
UserName = "user",
Password = "mypassword",
FtpSecure = FtpSecure.Explicit, // Or .Implicit
};
// Configure proxy
sessionOptions.AddRawSettings("ProxyMethod", "3");
sessionOptions.AddRawSettings("ProxyHost", "proxy");
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
var listing = session.ListDirectory(path);
}
有關(guān) SessionOptions.AddRawSettings
的選項(xiàng),請(qǐng)參閱 原始設(shè)置.
(我是 WinSCP 的作者)
這篇關(guān)于在 C# 中使用代理連接到 FTPS的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!