【问题标题】:SSL TLS 1.2 Channel creation VB.net HTTPS WebRequestSSL TLS 1.2 通道创建 VB.net HTTPS WebRequest
【发布时间】:2017-03-15 17:59:40
【问题描述】:

我有一个旧版 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

提前感谢您的建议!

【问题讨论】:

  • 我认为 .NET 4.5 中添加了 TLS 1.2 支持,因此您需要安装它,然后使用 ServicePointManager.SecurityProtocol = DirectCast(3072, SecurityProtocolType) 解决方法来处理 4.0 代码。
  • 是的,在我的回答中,我根据这些信息进一步走了几步来完成我的任务。

标签: vb.net ssl webrequest


【解决方案1】:

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

ServicePointManager.SecurityProtocol = DirectCast(3072, SecurityProtocolType)

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2018-04-06
      • 2017-05-25
      • 2020-12-15
      • 2019-06-17
      • 2016-02-07
      • 2017-05-27
      • 1970-01-01
      相关资源
      最近更新 更多