简体   繁体   中英

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.

Error: The request was aborted: Could not create SSL/TLS secure channel.

    '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.

All help are really appreciated. TIA

Obviously the URL you're calling requires TLS 1.1 or TLS 1.2 .

You can enable TLS 1.1 or TLS 1.2 by setting the security-protocol with ServicePointManager.SecurityProtocol :

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

.NET 4.0 supports up to TLS 1.0 while .NET 4.5 or higher supports up to 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. The same piece of code runs in both an .exe, and in IIS as a web page.

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.

Forcing the TLS protocol fixed it in the website.

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.

for vb.net 4.0 this worked for me: ServicePointManager.SecurityProtocol = DirectCast(&HC0 Or &H300 Or &HC00, SecurityProtocolType)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM