469,300 Members | 2,190 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,300 developers. It's quick & easy.

find External IP Address in VB.NET

115 100+
i'm using VB.NET 2003 Application program. i need to get External IP Address (internet). i searched internet and found come codes and tried that...

i tried this code... but it returned my internal IP Address...
Expand|Select|Wrap|Line Numbers
  1.  Dim IPHost As IPHostEntry = Dns.GetHostByName(Dns.GetHostName())
  2.  MessageBox.Show("My IP address is " & IPHost.AddressList(0).ToString())

and i tried this code too...
Expand|Select|Wrap|Line Numbers
  1. Imports System
  2. Imports System.Text
  3. Imports System.Text.RegularExpressions
  5. Public Sub GetExternalIP()
  6.         Dim whatIsMyIp As String = "http://whatismyip.com"
  7.         Dim getIpRegex As String = "(?<=<TITLE>.*)\d*\.\d*\.\d*\.\d*(?=</TITLE>)"
  8.         Dim wc As WebClient = New WebClient
  9.         Dim utf8 As UTF8Encoding = New UTF8Encoding
  10.         Dim requestHtml As String = ""
  11.         Dim externalIp As IPAddress = Nothing
  13.         requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp))
  15.         Dim r As Regex = New Regex(getIpRegex)
  16.         Dim m As Match = r.Match(requestHtml)
  17.         If (m.Success) Then
  18.             externalIp = IPAddress.Parse(m.Value)
  19.             MessageBox.Show(externalIp.ToString)
  20.         End If
  21. End Sub
but its always returns (m.fail) instead of (m.Success). so i'm not able to get External Ip address.

using command window - Immediate, i get values for "r" and "m". and m.success = false...
? r
Options: None
RightToLeft: False

? r.Match(requestHtml)
Captures: {System.Text.RegularExpressions.CaptureCollection}
Empty: {System.Text.RegularExpressions.Match}
Groups: {System.Text.RegularExpressions.GroupCollection}
Index: 0
Length: 0
Success: False
Value: ""
i don't have fire wall setup in my machine. and i have internet access too...

anything wrong in that code.. or anything i'm missing... if anyone have any idea how to find out the External Ip Address, please help me. if you can provide an example, then it will be a great help for me.

Thanks in advance.
Dec 5 '08 #1
4 11971
7,872 Expert 4TB
Using a screen scraping method from a site like What's My Ip is probably the best way to do it.
You would need to understand what was going on in the code to make that work.
Dec 5 '08 #2
Curtis Rutland
3,256 Expert 2GB
Use this page for WhatIsMyIP. They don't want you loading the front page programatically. The page I linked returns nothing but an IP. Here's why you should use that page:
Some automators are hitting our home page really hard eating up a lot of bandwidth and possibly causing the site to be slow for other visitors. So we've created an Automation page. This file should make it cleaner for you and easier on our bandwidth. Even though it'll be lighter on the bandwidth, please ONLY hit this page at a reasonable pace. Some of you are hitting the site at a pace of 2 times per second...that's WAY TOO often. We prefer you only hit it once per every 5 minutes, but if you need it more often than that, contact us and we can work something out.
Use this code:
Expand|Select|Wrap|Line Numbers
  1. using System.Net;
  2. using System.IO;
  3. .
  4. .
  5. .
  6. HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://whatismyip.com/automation/n09230945.asp");
  7. HttpWebResponse res = (HttpWebResponse)req.GetResponse();
  8. Stream stream = res.GetResponseStream();
  9. StreamReader sr = new StreamReader(stream);
  10. string ip = sr.ReadToEnd();
The first step creates a web request to the linked page. Then you get the response, and get the stream from the response. Using a StreamReader, I read the stream to the end, and now I have an IP string.
Dec 5 '08 #3
115 100+
Thank a lot to Plater and Insert Alias, that code worked... its working....

Expand|Select|Wrap|Line Numbers
  1. Dim req As HttpWebRequest = WebRequest.Create("http://whatismyip.com/automation/n09230945.asp")
  2.         Dim res As HttpWebResponse = req.GetResponse()
  3.         Dim Stream As Stream = res.GetResponseStream()
  4.         Dim sr As StreamReader = New StreamReader(Stream)
  5.         messagebox.show(sr.ReadToEnd())
thanks a lot for this help.... thanks a lot.....
Dec 5 '08 #4
Curtis Rutland
3,256 Expert 2GB
No problem. Glad we could help.
Dec 5 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

47 posts views Thread by Richard Hayden | last post: by
2 posts views Thread by Tim | last post: by
1 post views Thread by Lorne Smith | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.