在vb.net中获取IP地址

时间:2010-09-09 05:57:16

标签: vb.net ip-address ip

我想在vb.net中获取当前的互联网IP地址。我不想要localhost的ip地址。像(http://www.ipchicken.com/网站返回IP地址)

6 个答案:

答案 0 :(得分:2)

我明白您想要的是您在互联网上显示的IP地址,而不是您的内部网络IP。我已经在某个地方找到了一些我会为你挖掘的代码,但它基本上由此构成(来自内存):

就像我说的那样,我会尝试挖出代码,但这应该足以让你继续工作: - )

答案 1 :(得分:1)

这样的事情应该得到你当前的公共IP。

Public Class Form1

    Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        AddHandler wb.DocumentCompleted, AddressOf wb_DocumentCompleted
    End Sub

    Public WithEvents wb As New WebBrowser
    'before using wb add
    'AddHandler wb.DocumentCompleted, AddressOf wb_DocumentCompleted
    Private Sub GetPubIP()
        Try
            'the site returns a string like "Current IP Address: 69.59.196.211"
            wb.Navigate(New Uri("http://checkip.dyndns.org"))
        Catch ex As Exception
            'add error checking
        End Try
    End Sub

    Private Sub wb_DocumentCompleted(ByVal sender As Object, _
                                     ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
        'parse the reply
        Dim parts() As String = wb.Document.Body.InnerText.Split(":"c)
        Debug.WriteLine(parts(1).Trim)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) Handles Button1.Click
        GetPubIP()
    End Sub
End Class

答案 2 :(得分:1)

这就是你要找的东西。我写了一个简单的PHP脚本来调用时返回IP地址,然后编写这个VB.Net代码随时调用它:

   Public Function jnWhatIsMyExternalIP() As String
      Dim strURL As String = "http://www.mycompanywebsite/jnNetworkTools/jnCheckIP.php"

      Dim Request As System.Net.WebRequest = System.Net.WebRequest.Create(strURL)
      Dim Response As System.Net.WebResponse = Request.GetResponse()

      Dim Reader As New System.IO.StreamReader(Response.GetResponseStream())
      Dim strMyIP As String = Reader.ReadToEnd()

      Return strMyIP
   End Function

这是jnCheckIP.php中的PHP代码:

<?php
echo $_SERVER['REMOTE_ADDR'];
?>

调用VB.Net函数会导致用户计算机调用服务器上的PHP脚本,然后返回用户的IP地址。

答案 3 :(得分:0)

只有当您的主机(pc)具有静态互联网IP地址,而不是像194.x.x.x或10.x.x.x以及其他http://en.wikipedia.org/wiki/Private_network时,您才能知道真正的IP地址 否则尝试向www.ipchicken.com或其他http://www.google.ru/search?hl=en&q=my+ip+address等服务发送http请求以了解您的IP地址

答案 4 :(得分:0)

答案 5 :(得分:0)

Function GetIP() As String
    Dim IP As New WebClient
    Return IP.DownloadString("http://icanhazip.com/")
End Function