[英]VB. NET: The request was aborted: Could not create SSL/TLS secure channel
I have an application coded in VB.net that has this method of accessing Webservice, i have this error and after searching fixes i still have no luck.我有一个在 VB.net 中编码的应用程序,它具有这种访问 Web 服务的方法,我有这个错误,在搜索修复后我仍然没有运气。
Error: The request was aborted: Could not create SSL/TLS secure channel.错误:请求被中止:无法创建 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
This is my code and I'm using VS2015 and Windows 10.这是我的代码,我使用的是 VS2015 和 Windows 10。
All help are really appreciated.非常感谢所有帮助。 TIA
TIA
Obviously the URL you're calling requires TLS 1.1
or TLS 1.2
.显然,您调用的 URL 需要
TLS 1.1
或TLS 1.2
。
You can enable TLS 1.1
or TLS 1.2
by setting the security-protocol with ServicePointManager.SecurityProtocol
:您可以通过使用
ServicePointManager.SecurityProtocol
设置安全协议来启用TLS 1.1
或TLS 1.2
:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
.NET 4.0
supports up toTLS 1.0
while.NET 4.5
or higher supports up toTLS 1.2
.NET 4.0
最高支持TLS 1.0
而.NET 4.5
或更高版本最高支持TLS 1.2
For reference:供参考:
I'm going to add something here in case it helps others.我将在这里添加一些东西,以防它对其他人有帮助。 I have to support some legacy code that is written in VB.Net and targets .NET Framework 4.6.1.
我必须支持一些用 VB.Net 编写并针对 .NET Framework 4.6.1 的遗留代码。 The same piece of code runs in both an .exe, and in IIS as a web page.
同一段代码在 .exe 和 IIS 中作为网页运行。
In the website, this call:在网站上,这个电话:
Dim dataStream As Stream = request.GetRequestStream()
would throw this exception会抛出这个异常
The request was aborted: Could not create SSL/TLS secure channel.
but it worked fine in the .exe.但它在 .exe 中运行良好。
Forcing the TLS protocol fixed it in the website.强制使用 TLS 协议在网站上修复了它。
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
Of course if the host ever deems TLS 1.2 unworthy I'll have to fix/recompile/redeploy, but it solved the error for now.当然,如果主机认为 TLS 1.2 不值得,我将不得不修复/重新编译/重新部署,但它现在解决了错误。
for vb.net 4.0 this worked for me: ServicePointManager.SecurityProtocol = DirectCast(&HC0 Or &H300 Or &HC00, SecurityProtocolType)对于 vb.net 4.0 这对我有用:ServicePointManager.SecurityProtocol = DirectCast(&HC0 Or &H300 Or &HC00, SecurityProtocolType)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.