下载WPF 4.0中的图像失败 [英] Downloading Image Failed in WPF 4.0

查看:79
本文介绍了下载WPF 4.0中的图像失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在开发简单的图像下载程序wpf应用程序。我已经尝试了所有方法来下载图像。但没有任何效果。该问题特定于该图像网址。


获取错误: 基础连接已关闭:发送时发生意外错误。


内部异常详细信息: 无法从传输连接读取数据:远程主机强制关闭现有连接。  



下载样本
并检查。注意:使用WebClient API正在使用Dot Net版本4.6.1版本。我不知道发现这个问题。请帮忙......


谢谢,

解决方案

哈希,


根据您的代码示例,我替换了您的网址,它可以正常工作,因此在webclient或httpclient中使用https时会出现此错误消息。  


原因是有问题的网站仅支持TLS 1.2。在.NET中,System.Net.ServicePointManager.SecurityProtocol的默认值为Ssl | Tls,这意味着默认情况下.NET客户端不支持Tls 1.2(它在SSL协商期间不会在
支持的协议列表中列出此协议)。至少对于许多.NET Framework版本来说就是这种情况,不确定是否适用于所有版本。但.NET确实支持TLS 1.2。


你应该让你的应用程序.NETframework版本至少4.5使用它并启用它,你应该这样做:

 
var client = new WebClient();
// var result = client.DownloadData(imageSource);
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls | System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12;
var result = client.DownloadData(imageSource);
//client.DownloadFile(imageSource," D:\\Work \\\\ult.jpg");
// var stream = client.OpenRead(imageSource);
UpdateImage(result);

最好的问候,


Cherry



Hi All,

I am developing simple image downloader wpf app. I have tried all the ways to get the image downloaded. but nothing worked. The issue is specific to that image url.

Getting error: The underlying connection was closed: An unexpected error occurred on a send.

Inner Exception Details: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.  

Please download the sample and check. Note: With WebClient API is working in Dot Net version 4.6.1 version. I have no clue to find this issue. Please help...

Thanks,

解决方案

Hi harish,

According to your code sample, I replace your url and it can works fine, so this error message will appear when using https in webclient or httpclient.  

The reason is site in question supports only TLS 1.2. In .NET, default value for System.Net.ServicePointManager.SecurityProtocol is Ssl | Tls, which means that .NET client by default does not support Tls 1.2 (it does not list this protocol in the list of supported protocols during SSL negotiation). At least this is the case for many .NET Framework versions, not sure if for all. But .NET really do support TLS 1.2.

You should make your application .NETframework version at least 4.5 to use it and to enable it ,you should just do:

  
                var client = new WebClient();
                //var result = client.DownloadData(imageSource);
                System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls | System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12;
                var result = client.DownloadData(imageSource);
                //client.DownloadFile(imageSource, "D:\\Work\\result.jpg");
                //var stream = client.OpenRead(imageSource);           
                UpdateImage(result);

Best Regards,

Cherry


这篇关于下载WPF 4.0中的图像失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆