Get ip address in vb.net
I want to get current internet ip address in vb.net. I don't want localho开发者_JS百科st ip address. like as (http://www.ipchicken.com/ website return ip address)
I understand what you are wanting is the IP address as you appear on the internet and NOT your internal network IP. I've got some code somewhere I'll dig out for you but it basically comprised of this (from memory):
- Create a WebRequest and WebResponse object
- Request a webpage (using WebRequest) that shows your IP (such as IP Chicken) and capture the response in the WebResponse object
- Parse the response using regular expressions to collect your IP
Like I say I'll try and dig out the code but this should be more than enough information for you to work on :-)
Something like this should get your current public 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
Here's what you're looking for. I wrote a simple PHP script to return the IP address when called, then wrote this VB.Net code to call it at any time:
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
Here's the PHP code that is in jnCheckIP.php:
<?php
echo $_SERVER['REMOTE_ADDR'];
?>
Calling the VB.Net function causes the users machine to call the PHP script on your server which then returns the users IP address.
You can know real ip address only if your host (pc) have static internet ip address, not like 194.x.x.x or 10.x.x.x and other http://en.wikipedia.org/wiki/Private_network Otherwise try to send http request to service like www.ipchicken.com or other http://www.google.ru/search?hl=en&q=my+ip+address to know your ip address
This should do the trick:
http://www.vbdotnetheaven.com/UploadFile/prvn_131971/ipvb11162005073000AM/ipvb.aspx
Function GetIP() As String
Dim IP As New WebClient
Return IP.DownloadString("http://icanhazip.com/")
End Function
精彩评论