C# 使用SSL的WebRequest
我有以下代码来使用FTP检索文件(工作正常) 但是,当我尝试使用SSL执行此操作时,我无法访问该文件,如下所示:C# 使用SSL的WebRequest,c#,.net,ssl,ftp,webrequest,C#,.net,Ssl,Ftp,Webrequest,我有以下代码来使用FTP检索文件(工作正常) 但是,当我尝试使用SSL执行此操作时,我无法访问该文件,如下所示: FtpWebRequest request = (FtpWebRequest)WebRequest.Create(svrPath); request.KeepAlive = true; request.UsePassive = true; request.UseBinary = true
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
属性那样简单。您需要提供一个证书验证方法。请参阅,以获取执行所需操作的C#代码。此外,如果您需要更详细的实现,也有人复制并粘贴了他们的整个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
}
试试这个
FtpWebRequest请求=(FtpWebRequest)FtpWebRequest.Create(svrPath) 错误号/消息是什么?抱歉-我已经编辑了我的问题。您是否尝试过使用ssl与任何其他应用程序连接到ftp,以确认它已启用ssl?是否有方法根据具体情况执行此操作?这似乎是全球性的。
public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) {
return true; // Read the links provided above for real implementation
}