在 C# 中使用代理连接到 FTPS [英] Connect to FTPS with proxy in C#

查看:34
本文介绍了在 C# 中使用代理连接到 FTPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面的代码在我的电脑上运行得很好,没有代理.但是在客户端服务器中,他们需要向 FTP 客户端 (FileZilla) 添加代理才能访问 FTP.但是当我添加代理时它说

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

使用代理时无法启用 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 框架确实不支持通过代理的 TLS/SSL 连接.

.NET framework indeed does not support TLS/SSL connections over proxy.

您必须使用第 3 方 FTP 库.

You have to use a 3rd party FTP library.

另外请注意,您的代码没有使用隐式"FTPS.它使用显式"FTPS..NET 框架也不支持隐式 FTPS.

例如,对于 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);
}

有关 SessionOptions.AddRawSettings 的选项,请参阅 原始设置.

(我是 WinSCP 的作者)

这篇关于在 C# 中使用代理连接到 FTPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆