簡體   English   中英

ASP.NET“請求被中止:無法創建 SSL/TLS 安全通道”也出現在配置的 servicepointmanager 中

[英]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 證書。

我在這里檢查: 請求被中止:無法創建 SSL/TLS 安全通道

所以我將該解決方案添加到我的代碼中:

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 HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\SecurityProviders\\SCHANNEL\\KeyExchangeAlgorithms\\Diffie-Hellman.

但它仍然失敗......為什么?

這是我使用的返回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 framework的新項目。 然后我使用了這個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

這是我輸出的證明......

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM