Getting an External IP Address Locally


This article shall address the easiest way possible to get your external IP address (and local/internal IP address).


Figure 1:  Showing the Local and External IP Addresses

There is no particular magic to getting your external IP address; the easiest way I have found is to use the automation service provided by, the good folks at are nice enough to provide a free service to return the external IP address.  All that the ask in return is that you do not beat them up with too many requests; they suggest about one every five minutes as being within their comfort zone; however, they also offer to provide additional services upon request so if you need something more than that, give them a call and talk to them about it.

Now, if you have a public site and are interested in getting the user's IP address that is really not an issue.  You can still just request the REMOTE_ADDR or HTTP_X_FORWARDED_FOR server variable to get that.


There is not much code to address in this one.  There are three examples to show:  the local IP address, the external IP address (for when you might need to see that locally), and the remote IP address.

The first example shows how to collect the local IP address; here we use an instance of the IPHostEntry container class to capture the host entry and then we display the first item in the host entry's address list as the local IP address.

// Local IP Address (returns your internal IP address)

String hostName = System.Net.Dns.GetHostName();

IPHostEntry localIpAddresses = Dns.GetHostEntry(hostName);


Response.Write("<em>Your Local IP Address is:  " + localIpAddresses.AddressList[0].ToString() + "</em><br />");

The next example uses the automation service to capture the external IP address locally.  This value is displayed as the external IP address.  If you just need to find out your external IP address, you can use a browser to navigate to the website and you will see your external IP address displayed on the page; alternatively, you can use another service such as to perform the same function.  If you want to programmatically capture the external IP address locally, this code will do the job.

// External IP Address (get your external IP locally)

UTF8Encoding utf8 = new UTF8Encoding();

WebClient webClient = new WebClient();

String externalIp = utf8.GetString(webClient.DownloadData(



Response.Write("<h2>Your External IP Address is: " + externalIp + "</h2><br />");

If you are working from a public website and you want to capture the user's IP address; the following code will do the job.

// Remote IP Address (useful for getting user's IP from public site; run locally it just returns localhost)

string remoteIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

if (String.IsNullOrEmpty(remoteIpAddress))


remoteIpAddress = Request.ServerVariables["REMOTE_ADDR"];



Response.Write("<em>Your Remote IP Address is:  " + remoteIpAddress + "</em><br />");


This article was intended to show some simple ways to capture IP address information under different circumstances.  The three examples show demonstrate how to capture the local IP address, how to capture the external IP address locally, and how to capture the user's IP address from a public website.

Up Next
    Ebook Download
    View all
    View all