我正在尝试请求这张图片: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”不是“System.Net.SecurityProtocolType”的成员
并且
“Tls11”不是“System.Net.SecurityProtocolType”的成员
我还尝试更改注册表以允许 Windows 不阻止具有 512 位的 DHE,并在 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\KeyExchangeAlgorithms\Diffie-Hellman 下添加值为 0x00000200(512) 的 ClientMinKeyBitLength .
但还是失败了……为什么?
最佳答案
这是我使用的一个返回 Stream
的解决方案...您还可以修改它,使其返回一个字节数组,然后从该字节数组创建一个新的 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。 现在你的协议(protocol)应该是这样的......
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
这是我输出的证明...
关于vb.net - ASP.NET "The request was aborted: Could not create SSL/TLS secure channel"也与配置的 servicepointmanager 一起出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36120236/