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

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

''' <summary>
    ''' 连接外网
    ''' </summary>
    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

    ''' <summary>
    ''' 断开外网连接
    ''' </summary>
    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

    ''' <summary>
    ''' 获得外网IP
    ''' </summary>
    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数据包,才写了出来,后来发现网上有类似的,大概算是”英雄所见略同“吧。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值