【问题标题】:Cannot create SSL/TLS connection in vb when trying to obtain IPv4 address尝试获取 IPv4 地址时无法在 vb 中创建 SSL/TLS 连接
【发布时间】:2017-10-16 11:56:23
【问题描述】:

我正在计算机科学课上编写程序,但在尝试获取计算机的公共 IPv4 地址时遇到错误。

这是我的代码:

Private Function GetMyIP() As Net.IPAddress
    Using wc As New Net.WebClient

        Return Net.IPAddress.Parse(Encoding.ASCII.GetString(wc.DownloadData("http://tools.feron.it/php/ip.php")))


    End Using
End Function

然后使用以下代码调用它:

tboxPublicIPv4.Text = GetMyIP().ToString

但是,当它尝试将 IPv4 地址写入文本框时,我收到此错误:

An unhandled exception of type 'System.Net.WebException' occurred in System.dll

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

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: vb.net ssl system.net.webexception


    【解决方案1】:

    您调用的 URL 正在重定向到 https,并且似乎至少需要 TLS 1.1

    您可以通过使用ServicePointManager.SecurityProtocol 设置安全协议来为Net.WebClient 启用TLS 1.1TLS 1.2

    此外,您可以使用DownloadString 而不是将下载的数据转换为字符串。
    我也会把它包裹在 Try/Catch 中。

    Function GetMyIP() As Net.IPAddress
        Using wc As New Net.WebClient
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
    
            Try
                Return Net.IPAddress.Parse(wc.DownloadString("https://tools.feron.it/php/ip.php"))
            Catch ex As Exception
                Return New Net.IPAddress(0)
            End Try
        End Using
    End Function
    

    .NET 4.0 最高支持TLS 1.0.NET 4.5 或更高 最多支持TLS 1.2
    供参考:

    【讨论】:

    • 我现在得到一个 System.NullReferenceException 附加信息“对象引用未设置为对象的实例”。这发生在第 36 行。 tboxPublicIPv4.Text = GetmyIP().ToString
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    • 1970-01-01
    相关资源
    最近更新 更多