简体   繁体   English

如何从WPF应用程序中不受信任的安全站点下载文件?

[英]How can I download a file from an untrusted secured site in a WPF application?

I have a file on a secured site I would like to download using a WPF application. 我在安全的站点上有一个文件,我想使用WPF应用程序下载该文件。 The file is a tab delimited text file at a URL of the form https://my.server.com/test/my_file.txt . 该文件是制表符分隔的文本文件,位于URL的形式为https://my.server.com/test/my_file.txt Here is the code that I have so far: 这是我到目前为止的代码:

Uri uri = new Uri("https://my.server.com/test/my_file.txt");
System.Net.WebClient client = new System.Net.WebClient();
try
{
    client.DownloadFile(uri, "my_file.txt");
}
finally
{
    client.Dispose();
}

However, this throws a System.Net.WebException with the message "The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel." 但是,这将引发System.Net.WebException,并显示消息“基础连接已关闭:无法为SSL / TLS安全通道建立信任关系”。

Attempting to reach the file in by web browser first brings up the warning that the certificate issued by the server is not trusted, but in most modern browsers you can step through and reach the file successfully. 尝试通过Web浏览器进入文件首先会发出警告,提示服务器颁发的证书不受信任,但是在大多数现代浏览器中,您可以单步执行并成功访问文件。

看一下ServicePointManager.ServerCertificateValidationCallback-它使您可以定义自定义验证方法(因此您可以忽略证书验证失败的任何部分)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM