Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C语言中的FTPS(通过SSL的FTP)#_C#_.net_Winforms - Fatal编程技术网

C# C语言中的FTPS(通过SSL的FTP)#

C# C语言中的FTPS(通过SSL的FTP)#,c#,.net,winforms,C#,.net,Winforms,我需要一些指导。我需要在C#中开发一个可定制的FTP,应该使用App.Config文件进行配置。此外,FTP还应根据配置文件将数据从任何客户端推送到任何服务器 如果有人能提供指导,如果有任何API或任何其他有用的建议,或者让我朝正确的方向前进,我将不胜感激。我们使用时效果良好。我们使用时效果良好。您可以使用;然而,这是相当低的水平。有一个更高级别的类,对于许多场景,它需要更少的代码;但是,默认情况下,它不支持FTP/SSL。幸运的是,您可以通过注册自己的前缀使WebClient与FTP/SSL一

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

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

我们使用时效果良好。

我们使用时效果良好。

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

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://”开头。需要注意的是,您必须指定
方法
参数,因为没有默认参数。例如

using (var webClient = new WebClient()) {
    // Note here that the second parameter can't be null.
    webClient.UploadFileAsync(uploadUri, WebRequestMethods.Ftp.UploadFile, fileName, state);
}
你可以使用;然而,这是相当低的水平。有一个更高级别的类,对于许多场景,它需要更少的代码;但是,默认情况下,它不支持FTP/SSL。幸运的是,您可以通过注册自己的前缀使
WebClient
与FTP/SSL一起工作:

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://”开头。需要注意的是,您必须指定
方法
参数,因为没有默认参数。例如

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

公认的答案确实有效。但我发现注册前缀、实现接口以及所有这些东西都太麻烦了,尤其是当您只需要一次传输时

不是很难使用。所以我认为对于一次性使用,最好是这样:

FtpWebRequest request =
    (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.EnableSsl = true;
request.Method = WebRequestMethods.Ftp.UploadFile;  

using (Stream fileStream = File.OpenRead(@"C:\local\path\file.zip"))
using (Stream ftpStream = request.GetRequestStream())
{
    fileStream.CopyTo(ftpStream);
}
关键是关键


有关其他场景,请参见:

公认的答案确实有效。但我发现注册前缀、实现接口以及所有这些东西都太麻烦了,尤其是当您只需要一次传输时

不是很难使用。所以我认为对于一次性使用,最好是这样:

FtpWebRequest request =
    (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.EnableSsl = true;
request.Method = WebRequestMethods.Ftp.UploadFile;  

using (Stream fileStream = File.OpenRead(@"C:\local\path\file.zip"))
using (Stream ftpStream = request.GetRequestStream())
{
    fileStream.CopyTo(ftpStream);
}
关键是关键


有关其他场景,请参见:

只是为了让人们知道免费版本不支持FTPS,专业版只是为了让人们知道免费版本不支持FTPS,专业版不支持以下注释:要自动接受客户端可能遇到的任何证书,此操作:
ServicePointManager.ServerCertificateValidationCallback+=(发件人、证书、链、sslPolicyErrors)=>正确;
@Sphinxxx请注意,盲目接受任何服务器证书都会使您容易受到中间人攻击。是的,这不是最优雅的解决方案。您有更安全的替代方案吗?通常,您只需确保服务器具有与其主机名匹配的SSL证书,就像对待任何HTTPS流量一样。如果您不能这样做,您可以。从以下注释中可以看出:要自动接受客户端可能遇到的任何证书,此操作有效:
ServicePointManager.ServerCertificateValidationCallback+=(发件人、证书、链、sslPolicyErrors)=>正确;
@Sphinxxx请注意,盲目接受任何服务器证书都会使您容易受到中间人攻击。是的,这不是最优雅的解决方案。您有更安全的替代方案吗?通常,您只需确保服务器具有与其主机名匹配的SSL证书,就像对待任何HTTPS流量一样。如果你不能那样做,你可以。