在 C# 中使用代理连接到 FTPS

Connect to FTPS with proxy in C#(在 C# 中使用代理连接到 FTPS)
本文介绍了在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)