VB。NET:请求被中止:无法创建 SSL/TLS 安全通道

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46825387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:20:42  来源:igfitidea点击:

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

vb.net

提问by wysiwyg327

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

回答by MatSnow

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

显然,您正在调用的 URL 需要TLS 1.1TLS 1.2

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

您可以启用TLS 1.1TLS 1.2通过设置安全协议ServicePointManager.SecurityProtocol

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

.NET 4.0supports up to TLS 1.0while .NET 4.5or higher supports up to TLS 1.2
For reference:

.NET 4.0支持高达TLS 1.0while.NET 4.5或更高 支持高达TLS 1.2
参考: