ASP.NET "The request was aborted: Could not create SSL/TLS secure channel" 也发生在已配置的服务点管理器中

ASP.NET "The request was aborted: Could not create SSL/TLS secure channel" occurs also with configured servicepointmanager

我正在尝试请求这张图片:https://www.kamerstunt.nl/file/img/web/woning/9577323WenumWieselZwolseweg-142d_6.jpg 如果在您阅读本文时该图像不再存在,它可能已被删除,但您仍然可以查看有问题的 SSL 证书。使用我的浏览器,我能够成功导航到页面、请求图像并看到有效的 SSL 证书。

我在这里查看:The request was aborted: Could not create SSL/TLS secure channel

所以我将该解决方案添加到我的代码中:

Dim imgRequest As WebRequest = WebRequest.Create("https://www.kamerstunt.nl/file/img/web/woning/9577323WenumWieselZwolseweg-142d_6.jpg")
Dim imgResponse As WebResponse
ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls 
'//I also tried: ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls Or SecurityProtocolType.Ssl3
imgResponse = imgRequest.GetResponse()
Dim streamPhoto As Stream = imgResponse.GetResponseStream()

我试过了:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 Or SecurityProtocolType.Tls Or SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls12

但是我得到了错误: 'Tls12' is not a member of 'System.Net.SecurityProtocolType''Tls11' is not a member of 'System.Net.SecurityProtocolType'

我还尝试更改注册表以允许 windows 不阻止具有 512 位的 DHE,并在 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\KeyExchangeAlgorithms\Diffie-Hellman.

下添加了值为 0x00000200(512) 的 ClientMinKeyBitLength

但还是失败了...为什么?

这是我使用的解决方案 returns a Stream...您也可以修改它,使其 returns 一个字节数组,然后从该字节创建一个新的 MemoryStream大批。您也可以将其更改为 url,而不是要传递的字符串类型,但这取决于您。

Public Function GetRemoteStream(uRL As String) As MemoryStream
   Dim webClient As New WebClient()
   Dim imageBytes As Byte() = webClient.DownloadData(uRL)
   Dim mem As New MemoryStream(imageBytes)
   Return mem
End Function

示例用法

 Dim nStream As New MemoryStream
 nStream = GetRemoteStream(yoururlhere)

编辑请阅读 ********************************* ************

在对此进行调查并进一步挖掘之后,我找到了解决方案。该站点似乎已放弃对 SSL 和 Tls11 的支持。

我开始了一个针对 4.5 框架 的新项目。然后我用这个 SecurityProtocolType...

 SecurityProtocolType.Tls12

解决方案

将目标框架更改为:4.5 并使用:SecurityProtocolType.Tls12。 现在你的协议应该是这样的...

 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

另注

我建议将您的 Stream 包裹起来,以便妥善处理。例如:

Using stream As Stream = imgResponse.GetResponseStream()
            Using ms As New MemoryStream()
                Dim count As Integer = 0
                Do
                    Dim buf As Byte() = New Byte(1023) {}
                    count = stream.Read(buf, 0, 1024)
                    ms.Write(buf, 0, count)
                Loop While stream.CanRead AndAlso count > 0
                'ms is your memory stream... as I take it you want the photo :)
            End Using
        End Using

这是我输出的证明...