我正在尝试申请此图片: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'
我还尝试更改注册表以允许窗口不用512位阻止DHE,并在HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\KeyExchangeAlgorithms\Diffie-Hellman.
但它仍然失败了......为什么?
答案 0 :(得分:1)
这是我使用的一个解决方案,它返回一个Stream
...您也可以修改它,使它返回一个字节数组,然后从该字节数组创建一个新的MemoryStream。也可以将其更改为网址,而不是要传递的字符串类型,但这取决于您。
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& S的支持。 Tls11。
我开始了一个针对 4.5框架的新项目。然后我使用了这个SecurityProtocolType
...
SecurityProtocolType.Tls12
<强>解决方案强>
将目标框架更改为: 4.5 并使用: SecurityProtocolType.Tls12 。 现在你的协议应该是这样的......
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
另一个注意事项
我建议包装Stream
,以便妥善处理。{1}}例如:
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
这是我输出的证据......