使用 SSL 的 Web 请求
我有以下代码来使用 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.
I have the following code to retrieve a file using FTP (which works fine).
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();
}
However, when I try to do this using SSL, I am unable to access the file, as follows:
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();
}
Can anyone tell me why the latter would not work?
EDIT:
I get the following exception:
The remote server returned an error: (530) Not logged in.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在哪里验证 SSL 证书?通过 FTP 连接执行 SSL 并不像设置
.EnableSsl
属性那么简单。您需要提供证书验证方法。请参阅本文了解 C# 代码的用途你想要的。另外,有人在这篇 MSDN 文章<中复制并粘贴了整个 FTP 类< /a> 如果您需要更详细的实现。只是为了让您快速启动并运行,请使用以下命令进行测试:
然后再进行测试:
Where do you validate the SSL certificate? Doing SSL over an FTP connection isn't quite as simple as setting the
.EnableSsl
property. You need to provide a certificate validation method. See this article for the C# code to do what you want. Also, someone copied and pasted their whole FTP class in this MSDN article if you need a more detailed implementation.Just to quickly get you up and running quickly, test with this:
and then later:
试试这个
FtpWebRequest 请求 = (FtpWebRequest)FtpWebRequest.Create(svrPath);
Try this
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(svrPath);