【问题标题】:Visual Basic NET cant connect to TLS 1.2 web serverVisual Basic NET 无法连接到 TLS 1.2 Web 服务器
【发布时间】:2020-06-16 20:56:12
【问题描述】:

我需要使用 Visual Basic NET 连接到本地 Web 服务器。 Web 服务器具有 TLS 1.2。首先,我需要使用用户名和密码连接到页面,然后我需要从将要打开的页面中获取一些值。我知道服务器一切正常,因为我可以连接 python 脚本。因此,不起作用的 Visual Basic 代码是:

Imports System.Net
Imports System.Net.Http
Imports Newtonsoft.Json


Public Class Main

    Dim Red As Integer

    Private Shared ReadOnly client As HttpClient
    Public Function AcceptAllCertificates(ByVal sender As Object, ByVal certification As System.Security.Cryptography.X509Certificates.X509Certificate, ByVal chain As System.Security.Cryptography.X509Certificates.X509Chain, ByVal sslPolicyErrors As System.Net.Security.SslPolicyErrors) As Boolean
        Return True
    End Function

    Shared Sub New()
        client = New HttpClient()
    End Sub

    Private Function GetSessionID() As String
        Dim form As MultipartFormDataContent = New MultipartFormDataContent()
        form.Add(New StringContent("Login"), "login")
        form.Add(New StringContent("Username"), "user")
        form.Add(New StringContent("Pasword"), "pwd")

        Try
            Dim response As HttpResponseMessage = client.PostAsync("main.app", form).Result

            Dim content As String = response.Headers.GetValues("Set-Cookie").FirstOrDefault()
            Dim parts() As String = content.Split(";")
            Dim value As String = parts(0)

            If value.Substring(0, 9) = "SessionId" Then
                Return value.Substring(10)
            End If
        Catch exception As Exception

        End Try

        Throw New Exception("Can't find the session ID")
    End Function

    Private Function GetTimeStamp() As Integer
        Return DateTime.UtcNow.Subtract(New DateTime(1970, 1, 1)).TotalSeconds()
    End Function

    Private Sub GetValue(ByVal sessionID As String)
        Dim response As HttpResponseMessage = client.GetAsync("ajax.app?SessionId=" & sessionID & "&service=getDp&plantItemId=00000&_=" & GetTimeStamp()).Result

        Dim data = JsonConvert.DeserializeAnonymousType(response.Content.ReadAsStringAsync().Result, New With {Key .value = "", .unit = ""})

        Text1.Text = data.value & " " & data.unit
    End Sub

    Private Sub Connect_Click(sender As Object, e As EventArgs) Handles Connect.Click
        Try
            Dim sessionID As String = GetSessionID()

            txtSessionID.Text = sessionID
            GetTemperature(sessionID)
        Catch exception As Exception
            MessageBox.Show(exception.ToString())
        End Try
    End Sub

    Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ServicePointManager.ServerCertificateValidationCallback = AddressOf AcceptAllCertificates

        'ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
        System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

        client.BaseAddress = New Uri("https://xxx.xxx.xxx.xxx/)

    End Sub

有效的 Python 代码是:

import requests

urllib3 = requests.urllib3 
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

session = requests.session()
session.verify=False

response = session.get('https://xxx.xxx.xxx.xxx/main.app?user=Username&pwd=Password')
response = session.get('https://xxx.xxx.xxx.xxx/ajax.app?service=getDp&plantItemId=00000&_=')

print(response.text)

感谢您的帮助。

【问题讨论】:

    标签: python visual-studio tls1.2


    【解决方案1】:

    .Net 有一个名为“useStrongCrypto”的设置,允许客户端 PC 使用 TLS 1.1 及更高版本。默认情况下,由于某些令人沮丧的原因,它会被关闭。

    要使您的本地客户端能够使用 TLS 1.1 及更高版本(或 Microsoft 称之为“强加密”),您需要编辑以下注册表项: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft.NETFramework\v4.0.30319\SchUseStrongCrypto = 00000001 HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft.NETFramework\v4.0.30319\SchUseStrongCrypto = 00000001

    这将需要为您安装的每个版本的 .NET Framework 完成。他们每个人都应该在最后一棵树中有一个注册表项。不要忘记重新启动以使更改生效并首先备份注册表。

    这里有更多信息:https://dougrathbone.com/blog/2016/02/28/pci-compliant-web-deploy-getting-webdeploy-working-after-disabling-insecure-ciphers-like-ssl-30-and-tls-10

    【讨论】:

    • 嗨,马特 L。谢谢您的回答。我为所有 NET Framework 版本编辑此注册表。但它不起作用,我收到此异常:ibb.co/WgT9RW1 地址正确。我不知道为什么会这样。提前谢谢你。
    • 尝试取消注释 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 我有其他代码使用这个确切的语句,它对我有用。
    • 我试过但没有帮助。你能把你的代码发给我吗,这样我只会根据我的需要改变吗?提前谢谢你。
    猜你喜欢
    • 1970-01-01
    • 2020-05-14
    • 2017-08-17
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 2010-12-08
    • 2021-11-25
    • 1970-01-01
    相关资源
    最近更新 更多