首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在C#中使用代理连接FTPS

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

Stack Overflow用户
提问于 2017-12-28 09:12:08
回答 1查看 3.8K关注 0票数 4

在没有代理的情况下,下面的代码在我的计算机中运行得非常好。但是在客户端服务器中,他们需要向FTP客户端(FileZilla)添加代理,以便能够访问FTP。但是当我添加代理时它说

使用代理时无法启用SSL。

FTP代理

代码语言:javascript
运行
复制
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连接

代码语言:javascript
运行
复制
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();
EN

回答 1

Stack Overflow用户

发布于 2017-12-28 10:34:22

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

你必须使用第三方FTP库。

还请注意,您的代码没有使用“隐式”FTPS。它使用的是“显式”FTPS。https://stackoverflow.com/q/1842186/850848也是。

例如,对于WinSCP .NET组装,您可以使用:

代码语言:javascript
运行
复制
// 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的作者)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48004666

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档