![](/img/trans.png)
[英]VB. NET: The request was aborted: Could not create SSL/TLS secure channel on WebClient
[英]VB. NET: The request was aborted: Could not create SSL/TLS secure channel
我有一個在 VB.net 中編碼的應用程序,它具有這種訪問 Web 服務的方法,我有這個錯誤,在搜索修復后我仍然沒有運氣。
錯誤:請求被中止:無法創建 SSL/TLS 安全通道。
'ServicePointManager.Expect100Continue = False
'ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
Dim ErrMsg As String = String.Empty
Try
Dim response As WebResponse = request.GetResponse()
Using responseStream As Stream = response.GetResponseStream()
Dim reader As New StreamReader(responseStream, Encoding.UTF8)
Return reader.ReadToEnd()
End Using
Catch ex As WebException
ErrMsg = ex.Message.ToString
MsgBox(ErrMsg)
Dim errorResponse As WebResponse = ex.Response
Using responseStream As Stream = errorResponse.GetResponseStream()
Dim reader As New StreamReader(responseStream, Encoding.GetEncoding("utf-8"))
' log errorText
Dim errorText As [String] = reader.ReadToEnd()
End Using
Throw
End Try
這是我的代碼,我使用的是 VS2015 和 Windows 10。
非常感謝所有幫助。 TIA
顯然,您調用的 URL 需要TLS 1.1
或TLS 1.2
。
您可以通過使用ServicePointManager.SecurityProtocol
設置安全協議來啟用TLS 1.1
或TLS 1.2
:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
.NET 4.0
最高支持TLS 1.0
而.NET 4.5
或更高版本最高支持TLS 1.2
供參考:
我將在這里添加一些東西,以防它對其他人有幫助。 我必須支持一些用 VB.Net 編寫並針對 .NET Framework 4.6.1 的遺留代碼。 同一段代碼在 .exe 和 IIS 中作為網頁運行。
在網站上,這個電話:
Dim dataStream As Stream = request.GetRequestStream()
會拋出這個異常
The request was aborted: Could not create SSL/TLS secure channel.
但它在 .exe 中運行良好。
強制使用 TLS 協議在網站上修復了它。
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
當然,如果主機認為 TLS 1.2 不值得,我將不得不修復/重新編譯/重新部署,但它現在解決了錯誤。
對於 vb.net 4.0 這對我有用:ServicePointManager.SecurityProtocol = DirectCast(&HC0 Or &H300 Or &HC00, SecurityProtocolType)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.