gpt4 book ai didi

c# - 使用 SSL 的网络请求

转载 作者:太空狗 更新时间:2023-10-30 01:11:14 24 4
gpt4 key购买 nike

我有以下代码可以使用 FTP 检索文件(效果很好)。

            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(svrPath);

request.KeepAlive = true;
request.UsePassive = true;
request.UseBinary = true;

request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(uname, passw);

using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
using (Stream responseStream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(responseStream))
using (StreamWriter destination = new StreamWriter(destinationFile))
{
destination.Write(reader.ReadToEnd());
destination.Flush();
}

但是,当我尝试使用 SSL 执行此操作时,我无法访问该文件,如下所示:

            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(svrPath);

request.KeepAlive = true;
request.UsePassive = true;
request.UseBinary = true;

// The following line causes the download to fail
request.EnableSsl = true;

request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(uname, passw);

using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
using (Stream responseStream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(responseStream))
using (StreamWriter destination = new StreamWriter(destinationFile))
{
destination.Write(reader.ReadToEnd());
destination.Flush();
}

谁能告诉我为什么后者不起作用?

编辑:

我得到以下异常:

The remote server returned an error: (530) Not logged in.

最佳答案

您在哪里验证 SSL 证书?通过 FTP 连接执行 SSL 并不像设置 .EnableSsl 属性那么简单。您需要提供证书验证方法。参见 this article让 C# 代码做你想做的事。此外,有人在 this MSDN article 中复制并粘贴了他们的整个 FTP 类。如果您需要更详细的实现。

只是为了让你快速启动并快速运行,用这个测试:

if (request.EnableSsl) ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);

然后是:

public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) {
return true; // Read the links provided above for real implementation
}

关于c# - 使用 SSL 的网络请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3043461/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com