SSL TLS 1.2 通道创建 VB.net HTTPS WebRequest

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42817334/
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:13:20  来源:igfitidea点击:

SSL TLS 1.2 Channel creation VB.net HTTPS WebRequest

vb.netsslwebrequest

提问by Michael Wood

I have a legacy Visual Studio 2010 vb.net application that I need to update the SSL TLS channeling to support TLS 1.2. I have tried a couple of different options (see commented code for attempts) and all lead me to the same error, "Could not create SSL/TLS secure channel." What am I missing?

我有一个旧的 Visual Studio 2010 vb.net 应用程序,我需要更新 SSL TLS 通道以支持 TLS 1.2。我尝试了几个不同的选项(请参阅尝试的注释代码),并且都导致我出现相同的错误,“无法创建 SSL/TLS 安全通道。” 我错过了什么?

Public Shared Function processCCRequest(ByVal strRequest As String) As String
    'declare the web request object and set its path to the PayTrace API

    Dim ThisRequest As WebRequest = WebRequest.Create("https://beta.paytrace.com/api/default.pay")
    'configure web request object attributes
    ThisRequest.ContentType = "application/x-www-form-urlencoded"
    ThisRequest.Method = "POST"

    'encode the request
    Dim Encoder As New System.Text.ASCIIEncoding
    Dim BytesToSend As Byte() = Encoder.GetBytes(strRequest)

    'declare the text stream and send the request to PayTrace's API
    Dim StreamToSend As Stream = ThisRequest.GetRequestStream
    StreamToSend.Write(BytesToSend, 0, BytesToSend.Length)
    StreamToSend.Close()

    ''ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
    ''allows for validation of SSL conversations
    ''ServicePointManager.ServerCertificateValidationCallback = New System.Net.Security.RemoteCertificateValidationCallback(AddressOf)
    ServicePointManager.Expect100Continue = True
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
     ''| SecurityProtocolType.Tls11 | SecurityProtocolType.Tls
    ''var(response = WebRequest.Create("https://www.howsmyssl.com/").GetResponse())
    ''var(body = New StreamReader(response.GetResponseStream()).ReadToEnd())


    'Catch the response from the webrequest object
    Dim TheirResponse As HttpWebResponse = ThisRequest.GetResponse

    Dim sr As New StreamReader(TheirResponse.GetResponseStream)
    Dim strResponse As String = sr.ReadToEnd

    'Out put the string to a message box - application should parse the request instead
    ' MsgBox(strResponse)

    sr.Close()
    Return strResponse
End Function

Thank you in advance for your suggestions!

预先感谢您的建议!

采纳答案by Michael Wood

I was completely successful after downloading and installing the .net 4.6 Targeting Pack from Microsoft. This added the full .net upgrade that includes TLS 1.2 support and added .Net 4.6 as a publishing option. Once I upgraded and published, the code above worked with the security protocol set to TLS 1.2.

从 Microsoft 下载并安装 .net 4.6 Targeting Pack 后,我完全成功。这添加了完整的 .net 升级,包括 TLS 1.2 支持并添加了 .Net 4.6 作为发布选项。一旦我升级并发布,上面的代码就可以使用设置为 TLS 1.2 的安全协议。

回答by Yosep Tito

If You used .net 3.5, use this script

如果您使用 .net 3.5,请使用此脚本

ServicePointManager.SecurityProtocol = DirectCast(3072, SecurityProtocolType)