473,544 Members | 1,915 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

find External IP Address in VB.NET

115 New Member
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())
  3.  

and i tried this code too...
Expand|Select|Wrap|Line Numbers
  1. Imports System
  2. Imports System.Text
  3. Imports System.Text.RegularExpressions
  4.  
  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
  12.  
  13.         requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp))
  14.  
  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
  22.  
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
{System.Text.Re gularExpression s.Regex}
Options: None
RightToLeft: False

? r.Match(request Html)
{System.Text.Re gularExpression s.Match}
Captures: {System.Text.Re gularExpression s.CaptureCollec tion}
Empty: {System.Text.Re gularExpression s.Match}
Groups: {System.Text.Re gularExpression s.GroupCollecti on}
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 13015
Plater
7,872 Recognized Expert Expert
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 Recognized Expert Specialist
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
remya1000
115 New Member
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())
  6.  
thanks a lot for this help.... thanks a lot.....
Dec 5 '08 #4
Curtis Rutland
3,256 Recognized Expert Specialist
No problem. Glad we could help.
Dec 5 '08 #5

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

Similar topics

47
3805
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
1
1989
by: S. van Beek | last post by:
Dear reader, In case an application is ussing an external model library you have to address this external model library in your application. This addressing takes place in a module of the application by opening of a module and activates and than brows to the location of the external library.
10
2330
by: Sune | last post by:
Hi, previously I used Eclipse CDT for compiling my files just to get started with C and leave C++ behind. Now it's time to get a little more serious so I've moved my files to a new workplace and begun to use GNU Autotools. I'm sorry to say I'm new to gcc as well :( Now I get the most ridiculous compile error which I'm unable to solve....
2
5427
by: Tim | last post by:
Hi, I was wondering if anyone knows how to get the external IP address (the address beyond the router ) using C# code? Basically this is the address that appears on the internet. Thanks Tim
1
15146
by: Lorne Smith | last post by:
Hi, I'm running a small network and need to get the external IP address of my router so that I can update my DNS forwarding service should my IP change (it's a dynamic one)... I can get the IP addresses of all the machines in the network, and can get the internal IP address of the default gateway (the router), but I need to get the external...
12
1599
by: smerf | last post by:
I have searched high and low (and even in some places I'd like to forget I ever saw) for the information that will tell me exactly how the Nat2Nat server for UltraVNC works (not just a vague diagram, but the code needed - at least psuedocode - to connect the 2 clients) . (You can see it at http://www.uvnc.com/addons/nat2nat.html.) I get...
4
2636
by: fmaxwell | last post by:
Dear Group I have a very frustrating problem. I have been trying to make it more difficult to access external javascript files by using PHP sessions. This works beautifully locally (both in IE6 and IE7) and online in IE7, but frustratingly, online in IE6, the javascript does not render on the page, unless you refresh the page. Is...
1
9417
by: danknauf | last post by:
I have a webserver which is behaving strangely. The server is a fresh copy of Windows 2003 running IIS with default options + Active Server Pages installed. The server is connected to a Linksys router, and has a static IP address of 192.168.1.10. When I access a page with the following script directly from the server, my WAN IP address is...
2
9131
by: saritha2008 | last post by:
Hi, As part of transforming one form of xml to another form, i need to do the below mentioned transformation: My Input XML: <rss> <channel> <item> <assignee username="srinivas.rachakonda">Srinivas Rachakonda</assignee>
0
7597
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7752
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7358
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5894
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4902
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3396
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1831
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
650
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.