使用 SSL 的 Web 请求

发布于 2024-09-05 21:06:21 字数 1898 浏览 10 评论 0原文

我有以下代码来使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稍尽春風 2024-09-12 21:06:21

您在哪里验证 SSL 证书?通过 FTP 连接执行 SSL 并不像设置 .EnableSsl 属性那么简单。您需要提供证书验证方法。请参阅本文了解 C# 代码的用途你想要的。另外,有人在这篇 MSDN 文章<中复制并粘贴了整个 FTP 类< /a> 如果您需要更详细的实现。

只是为了让您快速启动并运行,请使用以下命令进行测试:

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
}

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:

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

and then later:

public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) {
 return true; // Read the links provided above for real implementation
}
花之痕靓丽 2024-09-12 21:06:21

试试这个
FtpWebRequest 请求 = (FtpWebRequest)FtpWebRequest.Create(svrPath);

Try this
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(svrPath);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文