C# 中的 FTPS(基于 SSL 的 FTP) [英] FTPS (FTP over SSL) in C#

查看:167
本文介绍了C# 中的 FTPS(基于 SSL 的 FTP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些指导.我需要在 C# 中开发一个可定制的 FTP,应该使用 App.Config 文件进行配置.此外,FTP 应该再次将数据从任何客户端推送到任何服务器,这取决于配置文件.

I need some guidance. I need to develop a customizable FTP in C# that should be configured using App.Config file. Also, the FTP should push the data to any server from any client again depends on config file.

如果有人可以指导,如果有任何 API 或任何其他有用的建议,或者让我朝着正确的方向前进,我将不胜感激.

I will appreciate if someone can guide, if there is any API or any other useful suggestion, or move me in the right direction.

推荐答案

您可以使用 FtpWebRequest;然而,这是相当低的水平.有一个更高级别的类 WebClient,它需要许多场景的代码少得多;但是,它默认不支持 FTP/SSL.幸运的是,您可以通过注册您自己的前缀使 WebClient 与 FTP/SSL 一起工作:

You can use FtpWebRequest; however, this is fairly low level. There is a higher-level class WebClient, which requires much less code for many scenarios; however, it doesn't support FTP/SSL by default. Fortunately, you can make WebClient work with FTP/SSL by registering your own prefix:

private void RegisterFtps()
{
    WebRequest.RegisterPrefix("ftps", new FtpsWebRequestCreator());
}

private sealed class FtpsWebRequestCreator : IWebRequestCreate
{
    public WebRequest Create(Uri uri)
    {
        FtpWebRequest webRequest = (FtpWebRequest)WebRequest.Create(uri.AbsoluteUri.Remove(3, 1)); // Removes the "s" in "ftps://".
        webRequest.EnableSsl = true;
        return webRequest;
    }
}

完成此操作后,您几乎可以像平常一样使用 WebClient,只是您的 URI 以ftps://"而不是ftp://"开头.一个警告是您必须指定 method 参数,因为不会有默认参数.例如

Once you do this, you can use WebClient almost like normal, except that your URIs start with "ftps://" instead of "ftp://". The one caveat is that you have to specify the method parameter, since there won't be a default one. E.g.

using (var webClient = new WebClient()) {
    // Note here that the second parameter can't be null.
    webClient.UploadFileAsync(uploadUri, WebRequestMethods.Ftp.UploadFile, fileName, state);
}

这篇关于C# 中的 FTPS(基于 SSL 的 FTP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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