【问题标题】:Connect to FTPS with proxy in C#在 C# 中使用代理连接到 FTPS
【发布时间】:2017-12-28 09:12:08
【问题描述】:

我的以下代码在我的计算机上运行良好,无需代理。但是在客户端服务器中,他们需要向 FTP 客户端 (FileZilla) 添加代理才能访问 FTP。但是当我添加代理时它说

使用代理时无法启用 SSL。

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();

【问题讨论】:

  • 这是否与常规 ftp 客户端连接?
  • @Saruman 是的

标签: c# .net ftp ftpwebrequest ftps


【解决方案1】:

.NET 框架确实不支持通过代理的 TLS/SSL 连接。

您必须使用第 3 方 FTP 库。

另外请注意,您的代码没有使用“隐式”FTPS。它使用“显式”FTPS。 Implicit FTPS is not supported by .NET framework 要么。


例如WinSCP .NET assembly,你可以使用:

// 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);
}

有关SessionOptions.AddRawSettings 的选项,请参阅raw settings

(我是 WinSCP 的作者)

【讨论】:

    猜你喜欢
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-14
    • 1970-01-01
    相关资源
    最近更新 更多