直接获得TP-LINK路由器外网IP地址

要获得路由器的外网(或公网)IP地址,一般的做法是先访问外网的一个网站(如,由这个网站记录下该路由器的外网IP地址,然后再返回结果。其实对于某些型号的路由器,我们完全可以直接读取它的外网IP,比如TP-LINK,下面是VB.NET实现代码,另附连接和断线两段代码:

''' 
    ''' 连接外网
    ''' 
    Public Sub Connect(ByVal UserName As String, ByVal Password As String)
        Dim xHttp As Object = CreateObject("MSXML2.XMLHTTP")
        xHttp.open("GET", "http://192.168.2.2/userRpm/StatusRpm.htm?Connect=连 接&wan=1", False, UserName, Password)
        xHttp.send()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xHttp)
        xHttp = Nothing
    End Sub

    ''' 
    ''' 断开外网连接
    ''' 
    Public Sub Disconnect(ByVal UserName As String, ByVal Password As String)
        Dim xHttp As Object = CreateObject("MSXML2.XMLHTTP")
        xHttp.open("GET", "http://192.168.2.2/userRpm/StatusRpm.htm?Disconnect=断 线&wan=1", False, UserName, Password)
        xHttp.send()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xHttp)
        xHttp = Nothing
    End Sub

    ''' 
    ''' 获得外网IP
    ''' 
    Public Function GetWanIp(ByVal UserName As String, ByVal Password As String) As String
        Dim strIP As String = "0.0.0.0"
        Dim xHttp As Object = CreateObject("MSXML2.XMLHTTP")
        xHttp.open("GET", "http://192.168.2.2/userRpm/StatusRpm.htm?Connect=连 接&wan=1", False, UserName, Password)
        xHttp.send()
        If xHttp.readyState = 4 AndAlso xHttp.Status = 200 Then
            Dim strHTML As String = Text.Encoding.Default.GetString(xHttp.responseBody)
            strHTML = strHTML.Substring(strHTML.IndexOf("var wanPara"))
            strHTML = strHTML.Substring(0, strHTML.IndexOf(";"))
            strIP = strHTML.Split(",")(2).Replace("""", "").Trim()
        End If
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xHttp)
        xHttp = Nothing

        Return strIP
    End Function
另,对于连接外网和断开外网连接操作,可能有的朋友已在某些博客中见到过,而我还在这儿标识为“原创”,原因是当初我未找到类似代码,通过拦截和分析HTTP数据包,才写了出来,后来发现网上有类似的,大概算是”英雄所见略同“吧。

你可能感兴趣的:(VB.NET)