94

I cant seem to get or find information on finding my routers public IP? Is this because it cant be done this way and would have to get it from a website?

2
  • Which way is "this way"? Are you trying to do this programmatically? Jul 15, 2010 at 8:21
  • 1
    With CGN, it is possible that your router does not have a public address.
    – Ron Maupin
    Mar 23, 2020 at 13:03

28 Answers 28

139

Using C#, With HTTPClient using async method

public static async Task<IPAddress?> GetExternalIpAddress()
{
    var externalIpString = (await new HttpClient().GetStringAsync("http://icanhazip.com"))
        .Replace("\\r\\n", "").Replace("\\n", "").Trim();
    if(!IPAddress.TryParse(externalIpString, out var ipAddress)) return null;
    return ipAddress;
}

public static void Main(string[] args)
{
    var externalIpTask = GetExternalIpAddress();
    GetExternalIpAddress().Wait();
    var externalIpString = externalIpTask.Result ?? IPAddress.Loopback;

    Console.WriteLine(externalIpString);
}

Obsolete C#, With WebClient its a short one.

public static void Main(string[] args)
{
    string externalIpString = new WebClient().DownloadString("http://icanhazip.com").Replace("\\r\\n", "").Replace("\\n", "").Trim();
    var externalIp = IPAddress.Parse(externalIpString);

    Console.WriteLine(externalIp.ToString());
}

Command Line (works on both Linux and Windows)

wget -qO- http://bot.whatismyipaddress.com

Curl

curl http://ipinfo.io/ip
5
  • 404 on the second one. Apr 16, 2014 at 12:15
  • 10
    Great answer! By the way, I had to call Trim() on the result from http://ipinfo.io/ip. There was a trailing space. Aug 1, 2014 at 17:32
  • 4
    This will get the server's IP address instead of the user's.
    – Paul
    Dec 21, 2018 at 0:44
  • What's the benefit of parsing the IP address, rather than outputting externalIpString directly?
    – Stewart
    Dec 20, 2021 at 12:51
  • 1
    Validation of the input, also you can use the IPAddress in some other method that requires such an input.
    – CodeOrElse
    Oct 26, 2022 at 13:03
74
static void Main(string[] args)
{
    HTTPGet req = new HTTPGet();
    req.Request("http://checkip.dyndns.org");
    string[] a = req.ResponseBody.Split(':');
    string a2 = a[1].Substring(1);
    string[] a3=a2.Split('<');
    string a4 = a3[0];
    Console.WriteLine(a4);
    Console.ReadLine();
}

Do this small trick with Check IP DNS

Use HTTPGet class i found on Goldb-Httpget C#

1
  • HttpGet does not exist , alternative ?
    – Kiquenet
    Jul 30, 2020 at 11:28
43
string pubIp =  new System.Net.WebClient().DownloadString("https://api.ipify.org");
1
  • Error: The request was aborted: Could not create SSL/TLS secure channel. Aug 11, 2023 at 8:38
40

With .Net WebRequest:

  public static string GetPublicIP()
    {
        string url = "http://checkip.dyndns.org";
        System.Net.WebRequest req = System.Net.WebRequest.Create(url);
        System.Net.WebResponse resp = req.GetResponse();
        System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
        string response = sr.ReadToEnd().Trim();
        string[] a = response.Split(':');
        string a2 = a[1].Substring(1);
        string[] a3 = a2.Split('<');
        string a4 = a3[0];
        return a4;
    }
29

Using a great similar service

private string GetPublicIpAddress()
{
    var request = (HttpWebRequest)WebRequest.Create("http://ifconfig.me");

    request.UserAgent = "curl"; // this will tell the server to return the information as if the request was made by the linux "curl" command

    string publicIPAddress;

    request.Method = "GET";
    using (WebResponse response = request.GetResponse())
    {
        using (var reader = new StreamReader(response.GetResponseStream()))
        {
            publicIPAddress = reader.ReadToEnd();
        }
    }

    return publicIPAddress.Replace("\n", "");
}
3
  • 1
    ifconfig.me this domain is perfect
    – yurenchen
    Jan 22, 2018 at 9:08
  • private string GetPublicIpAddress() { using var client = new WebClient(); return client.DownloadString("ifconfig.me").Replace("\n", ""); } Mar 23, 2020 at 11:56
  • 1
    A more up-to-date alternative: public static string GetPublicIPv4Address() => new System.Net.Http.HttpClient().GetStringAsync("http://ifconfig.me").GetAwaiter().GetResult().Replace("\n", ""); Mar 8, 2022 at 16:44
25

Expanding on this answer by @suneel ranga:

static System.Net.IPAddress GetPublicIp(string serviceUrl = "https://ipinfo.io/ip")
{
    return System.Net.IPAddress.Parse(new System.Net.WebClient().DownloadString(serviceUrl));
}

Where you would use a service with System.Net.WebClient that simply shows the IP address as a string and uses the System.Net.IPAddress object. Here are a few such services*:

* Some services were mentioned in this question and from these answers from superuser site.

1
  • 2
    +1 for adding the https in front. I don't know why most people in here consider normal to ask for external ip through http. By the way checkip.amazonaws.com supports ssl now.
    – Marinos An
    May 6, 2020 at 13:59
17

In theory your router should be able to tell you the public IP address of the network, but the way of doing this will necessarily be inconsistent/non-straightforward, if even possible with some router devices.

The easiest and still a very reliable method is to send a request to a web page that returns your IP address as the web server sees it. Dyndns.org provides a good service for this:

http://checkip.dyndns.org/

What is returned is an extremely simple/short HTML document, containing the text Current IP Address: 157.221.82.39 (fake IP), which is trivial to extract from the HTTP response.

2
  • No worries. I only say because often new members are not familiar with the system... thanks.
    – Noldorin
    Jul 15, 2010 at 15:35
  • 1
    That does not actually work to get the router address if the ISP is using CGN. It would only get the ISP router address.
    – Ron Maupin
    Jun 19, 2020 at 16:41
9

I found that http://checkip.dyndns.org/ was giving me html tags I had to process but https://icanhazip.com/ was just giving me a simple string. Unfortunately https://icanhazip.com/ gives me the ip6 address and I needed ip4. Luckily there are 2 subdomains that you can choose from, ipv4.icanhazip.com and ipv6.icanhazip.com.

        string externalip = new WebClient().DownloadString("https://ipv4.icanhazip.com/");
        Console.WriteLine(externalip);
        Console.WriteLine(externalip.TrimEnd());
2
  • This is the best way, it works and it is short code ! Aug 24, 2017 at 13:01
  • Great solution, however, it depends on some other external service. Would be great to find a couple other similar services and use as a fallback. E.g. api.ipify.org and trackip.net/ip
    – Kosta_M
    Feb 24, 2019 at 6:22
6

With a few lines of code you can write your own Http Server for this.

HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://+/PublicIP/");
listener.Start();
while (true)
{
    HttpListenerContext context = listener.GetContext();
    string clientIP = context.Request.RemoteEndPoint.Address.ToString();
    using (Stream response = context.Response.OutputStream)
    using (StreamWriter writer = new StreamWriter(response))
        writer.Write(clientIP);

    context.Response.Close();
}

Then anytime you need to know your public ip, you can do this.

WebClient client = new WebClient();
string ip = client.DownloadString("http://serverIp/PublicIP");
1
  • when does this ever exist, with that while loop?
    – ossentoo
    Feb 18, 2021 at 19:19
6

Basically I prefer to use some extra backups in case if one of IP is not accessible. So I use this method.

 public static string GetExternalIPAddress()
        {
            string result = string.Empty;
            try
            {
                using (var client = new WebClient())
                {
                    client.Headers["User-Agent"] =
                    "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0) " +
                    "(compatible; MSIE 6.0; Windows NT 5.1; " +
                    ".NET CLR 1.1.4322; .NET CLR 2.0.50727)";

                    try
                    {
                        byte[] arr = client.DownloadData("http://checkip.amazonaws.com/");

                        string response = System.Text.Encoding.UTF8.GetString(arr);

                        result = response.Trim();
                    }
                    catch (WebException)
                    {                       
                    }
                }
            }
            catch
            {
            }

            if (string.IsNullOrEmpty(result))
            {
                try
                {
                    result = new WebClient().DownloadString("https://ipinfo.io/ip").Replace("\n", "");
                }
                catch
                {
                }
            }

            if (string.IsNullOrEmpty(result))
            {
                try
                {
                    result = new WebClient().DownloadString("https://api.ipify.org").Replace("\n", "");
                }
                catch
                {
                }
            }

            if (string.IsNullOrEmpty(result))
            {
                try
                {
                    result = new WebClient().DownloadString("https://icanhazip.com").Replace("\n", "");
                }
                catch
                {
                }
            }

            if (string.IsNullOrEmpty(result))
            {
                try
                {
                    result = new WebClient().DownloadString("https://wtfismyip.com/text").Replace("\n", "");
                }
                catch
                {
                }
            }

            if (string.IsNullOrEmpty(result))
            {
                try
                {
                    result = new WebClient().DownloadString("http://bot.whatismyipaddress.com/").Replace("\n", "");
                }
                catch
                {
                }
            }

            if (string.IsNullOrEmpty(result))
            {
                try
                {
                    string url = "http://checkip.dyndns.org";
                    System.Net.WebRequest req = System.Net.WebRequest.Create(url);
                    System.Net.WebResponse resp = req.GetResponse();
                    System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
                    string response = sr.ReadToEnd().Trim();
                    string[] a = response.Split(':');
                    string a2 = a[1].Substring(1);
                    string[] a3 = a2.Split('<');
                    result = a3[0];
                }
                catch (Exception)
                {
                }
            }

            return result;
        }

In order to update GUI control (WPF, .NET 4.5), for instance some Label I use this code

 void GetPublicIPAddress()
 {
            Task.Factory.StartNew(() =>
            {
                var ipAddress = SystemHelper.GetExternalIPAddress();

                Action bindData = () =>
                {
                    if (!string.IsNullOrEmpty(ipAddress))
                        labelMainContent.Content = "IP External: " + ipAddress;
                    else
                        labelMainContent.Content = "IP External: ";

                    labelMainContent.Visibility = Visibility.Visible; 
                };
                this.Dispatcher.InvokeAsync(bindData);
            });

 }

Hope it is useful.

Here is an example of app that will include this code.

1
  • 3
    that cannot fail! :) Jan 23, 2017 at 7:12
5

checkip.dyndns.org is not always works correctly. For example, for my machine it shows internal after-NAT address:

Current IP Address: 192.168.1.120

I think its happening, because of I have my local DNS-zone behind NAT, and my browser sends to checkip its local IP address, which is returned back.

Also, http is heavy weight and text oriented TCP-based protocol, so not very suitable for quick and efficient regular request for external IP address. I suggest to use UDP-based, binary STUN, especially designed for this purposes:

http://en.wikipedia.org/wiki/STUN

STUN-server is like "UDP mirror". You looking to it, and see "how I looks".

There is many public STUN-servers over the world, where you can request your external IP. For example, see here:

http://www.voip-info.org/wiki/view/STUN

You can download any STUN-client library, from Internet, for example, here:

http://www.codeproject.com/Articles/18492/STUN-Client

And use it.

5

Fast way to get External ip without any connection Actualy no need any Http connection for that

first you must add NATUPNPLib.dll on Referance And select it from referances and check from properties window Embed Interop Type to False

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NATUPNPLib; // Add this dll from referance and chande Embed Interop Interop to false from properties panel on visual studio
using System.Net;

namespace Client
{
    class NATTRAVERSAL
    {
        //This is code for get external ip
        private void NAT_TRAVERSAL_ACT()
        {
            UPnPNATClass uPnP = new UPnPNATClass();
            IStaticPortMappingCollection map = uPnP.StaticPortMappingCollection;

            foreach (IStaticPortMapping item in map)
            {
                    Debug.Print(item.ExternalIPAddress); //This line will give you external ip as string
                    break;
            }
        }
    }
}
1
  • 4
    For NATUPNPLib to work requires UPnP to be enabled in the router, which is a security issue.
    – Reg Edit
    Jul 28, 2015 at 19:47
5

Answers based on using external web services are not exactly correct, because they do not actually answer the stated question:

... information on finding my routers public IP


Explanation

All online services return the external IP address, but it does not essentially mean, that this address is assigned to the user's router.

Router may be assigned with another local IP address of ISP infrastructure networks. Practically this means, that router can not host any services available on Internet. This may be good for safety of most home users, but not good for geeks who host servers at home.

Here's how to check if router has external IP:

trace route to web server behind ISP

According to Wikipedia article, the IP address ranges 10.0.0.0 – 10.255.255.255, 172.16.0.0 – 172.31.255.255 and 192.168.0.0 – 192.168.255.255 are used for private i.e. local networks.

See what happens when you trace route to some remote host with router being assigned with external IP address:

trace route to web server with external IP address

Gotcha! First hop starts from 31.* now. This clearly means that there's nothing between your router and Internet.


Solution

  1. Make Ping to some address with Ttl = 2
  2. Evaluate where response comes from.

TTL=2 must be not enough to reach remote host. Hop #1 host will emit "Reply from <ip address>: TTL expired in transit." revealing its IP address.

Implementation

try
{
    using (var ping = new Ping())
    {
        var pingResult = ping.Send("google.com");
        if (pingResult?.Status == IPStatus.Success)
        {
            pingResult = ping.Send(pingResult.Address, 3000, "ping".ToAsciiBytes(), new PingOptions { Ttl = 2 });

            var isRealIp = !Helpers.IsLocalIp(pingResult?.Address);

            Console.WriteLine(pingResult?.Address == null
                ? $"Has {(isRealIp ? string.Empty : "no ")}real IP, status: {pingResult?.Status}"
                : $"Has {(isRealIp ? string.Empty : "no ")}real IP, response from: {pingResult.Address}, status: {pingResult.Status}");

            Console.WriteLine($"ISP assigned REAL EXTERNAL IP to your router, response from: {pingResult?.Address}, status: {pingResult?.Status}");
        }
        else
        {
            Console.WriteLine($"Your router appears to be behind ISP networks, response from: {pingResult?.Address}, status: {pingResult?.Status}");
        }
    }
}
catch (Exception exc)
{
    Console.WriteLine("Failed to resolve external ip address by ping");
}

Small helper is used to check if IP belongs to private or public network:

public static bool IsLocalIp(IPAddress ip) {
    var ipParts = ip.ToString().Split(new [] { "." }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();

    return (ipParts[0] == 192 && ipParts[1] == 168) 
        || (ipParts[0] == 172 && ipParts[1] >= 16 && ipParts[1] <= 31) 
        ||  ipParts[0] == 10;
}
1
  • I only get local IPs this way :|
    – sinsedrix
    Feb 23, 2022 at 17:02
5

I find most of the other answers lacking as they assume that any returned string must be the IP, but doesn't really check for it. This is my solution that I'm currently using. It will only return a valid IP or null if none is found.

public class WhatsMyIp
{
    public static IPAddress PublicIp { get; private set; }
    static WhatsMyIp()
    {
        PublicIp = GetMyIp();
    }

    public static IPAddress GetMyIp()
    {
        List<string> services = new List<string>()
        {
            "https://ipv4.icanhazip.com",
            "https://api.ipify.org",
            "https://ipinfo.io/ip",
            "https://checkip.amazonaws.com",
            "https://wtfismyip.com/text",
            "http://icanhazip.com"
        };
        using (var webclient = new HttpClient())
            foreach (var service in services)
            {
                try { return IPAddress.Parse(webclient.GetStringAsync(service).Result); } catch { }
            }
        return null;
    }
}
3
  • That does not actually work to get the router address if the ISP is using CGN (becoming more common). It would only get the ISP router address, not the address assigned to your router.
    – Ron Maupin
    Jun 19, 2020 at 16:42
  • 1
    @RonMaupin Nobody is talking about the actual router address. The question is what is your IP on the Internet.
    – Jesper
    Jun 19, 2020 at 20:16
  • @Jesper you should update your answer to use HttpClient instead of WebClient
    – Codingwiz
    Jul 18, 2023 at 14:28
4
private static string GetPublicIpAddress()
{
    using (var client = new WebClient())
    {
       return client.DownloadString("http://ifconfig.me").Replace("\n", "");
    }
}
3

When I debug, I use following to construct the externally callable URL, but you could just use first 2 lines to get your public IP:

public static string ExternalAction(this UrlHelper helper, string actionName, string controllerName = null, RouteValueDictionary routeValues = null, string protocol = null)
{
#if DEBUG
    var client = new HttpClient();
    var ipAddress = client.GetStringAsync("http://ipecho.net/plain").Result; 
    // above 2 lines should do it..
    var route = UrlHelper.GenerateUrl(null, actionName, controllerName, routeValues, helper.RouteCollection, helper.RequestContext, true); 
    if (route == null)
    {
        return route;
    }
    if (string.IsNullOrEmpty(protocol) && string.IsNullOrEmpty(ipAddress))
    {
        return route;
    }
    var url = HttpContext.Current.Request.Url;
    protocol = !string.IsNullOrWhiteSpace(protocol) ? protocol : Uri.UriSchemeHttp;
    return string.Concat(protocol, Uri.SchemeDelimiter, ipAddress, route);
#else
    helper.Action(action, null, null, HttpContext.Current.Request.Url.Scheme)
#endif
}
1
  • for some reason the GetStringAsync() as u used it here is much faster for me using a HttpClient rather than a Get Request using WebRequest. i can confirm that this works like a charm.
    – Niklas
    May 4, 2017 at 11:53
3

I do it using HttpClient from System.Net.Http:

public static string PublicIPAddress()
{
    string uri = "http://checkip.dyndns.org/";
    string ip = String.Empty;

    using (var client = new HttpClient())
    {
        var result = client.GetAsync(uri).Result.Content.ReadAsStringAsync().Result;

        ip = result.Split(':')[1].Split('<')[0];
    }

    return ip;
}
2

Best answer I found

To get the remote ip address the quickest way possible. You must have to use a downloader, or create a server on your computer.

The downsides to using this simple code: (which is recommended) is that it will take 3-5 seconds to get your Remote IP Address because the WebClient when initialized always takes 3-5 seconds to check for your proxy settings.

 public static string GetIP()
 {
            string externalIP = "";
            externalIP = new WebClient().DownloadString("http://checkip.dyndns.org/");
            externalIP = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"))
                                           .Matches(externalIP)[0].ToString();
            return externalIP;
 }

Here is how I fixed it.. (first time still takes 3-5 seconds) but after that it will always get your Remote IP Address in 0-2 seconds depending on your connection.

public static WebClient webclient = new WebClient();
public static string GetIP()
{
    string externalIP = "";
    externalIP = webclient.DownloadString("http://checkip.dyndns.org/");
    externalIP = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"))
                                   .Matches(externalIP)[0].ToString();
    return externalIP;
}
2

I've refactored @Academy of Programmer's answer to shorter code and altered it so that it only hits https:// URLs:

    public static string GetExternalIPAddress()
    {
        string result = string.Empty;

        string[] checkIPUrl =
        {
            "https://ipinfo.io/ip",
            "https://checkip.amazonaws.com/",
            "https://api.ipify.org",
            "https://icanhazip.com",
            "https://wtfismyip.com/text"
        };

        using (var client = new WebClient())
        {
            client.Headers["User-Agent"] = "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0) " +
                "(compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";

            foreach (var url in checkIPUrl)
            {
                try
                {
                    result = client.DownloadString(url);
                }
                catch
                {
                }

                if (!string.IsNullOrEmpty(result))
                    break;
            }
        }

        return result.Replace("\n", "").Trim();
    }
}
1
1

You may use Telnet to programmatically query your router for the WAN IP.

The Telnet part

The Telnet part can be accomplished using, for example, this Minimalistic Telnet code as an API to send a Telnet command to your router and get the router's response. The remainder of this answer assumes you are set up in one way or another to send a Telnet command and get back the response in your code.

Limitations of approach

I will say up front that one drawback of querying the router compared to other approaches is that the code you write is likely to be fairly specific to your router model. That said, it can be a useful approach that doesn't rely on external servers, and you may anyway wish to access your router from your own software for other purposes, such as configuring and controlling it, making it more worthwhile writing specific code.

Example router command and response

The example below will not be right for all routers, but illustrates the approach in principle. You will need to change the particulars to suit your router commands and responses.

For example, the way to get your router to show the WAN IP may be the following Telnet command:

connection list

The output may consist of a list of lines of text, one per connection, with the IP address at offset 39. The line for the WAN connection may be identifiable from the word "Internet" somewhere in the line:

  RESP: 3947  17.110.226. 13:443       146.200.253. 16:60642     [R..A] Internet      6 tcp   128
<------------------  39  -------------><--  WAN IP -->

The output may pad each IP address segment out to three characters with spaces, which you will need to remove. (That is, in the xample above, you would need to turn "146.200.253. 16" into "146.200.253.16".)

By experimentation or consulting reference documentation for your router, you can establish the command to use for your specific router and how to interpret the router's response.

Code to get the WAN IP

(Assumes you have a method sendRouterCommand for the Telnet part—see above.)

Using the example router described above, the following code gets the WAN IP:

private bool getWanIp(ref string wanIP)
{
    string routerResponse = sendRouterCommand("connection list");

    return (getWanIpFromRouterResponse(routerResponse, out wanIP));
}

private bool getWanIpFromRouterResponse(string routerResponse, out string ipResult)
{
    ipResult = null;
    string[] responseLines = routerResponse.Split(new char[] { '\n' });

    //  RESP: 3947  17.110.226. 13:443       146.200.253. 16:60642     [R..A] Internet      6 tcp   128
    //<------------------  39  -------------><---  15   --->

    const int offset = 39, length = 15;

    foreach (string line in responseLines)
    {
        if (line.Length > (offset + length) && line.Contains("Internet"))
        {
            ipResult = line.Substring(39, 15).Replace(" ", "");
            return true;
        }
    }

    return false;
}
1

Most of the answers have mentioned http://checkip.dyndns.org in solution. For us, it didn't worked out well. We have faced Timemouts a lot of time. Its really troubling if your program is dependent on the IP detection.

As a solution, we use the following method in one of our desktop applications:

    // Returns external/public ip
    protected string GetExternalIP()
    {
        try
        {
            using (MyWebClient client = new MyWebClient())
            {
                client.Headers["User-Agent"] =
                "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0) " +
                "(compatible; MSIE 6.0; Windows NT 5.1; " +
                ".NET CLR 1.1.4322; .NET CLR 2.0.50727)";

                try
                {
                    byte[] arr = client.DownloadData("http://checkip.amazonaws.com/");

                    string response = System.Text.Encoding.UTF8.GetString(arr);

                    return response.Trim();
                }
                catch (WebException ex)
                {
                    // Reproduce timeout: http://checkip.amazonaws.com:81/

                    // trying with another site
                    try
                    {
                        byte[] arr = client.DownloadData("http://icanhazip.com/");

                        string response = System.Text.Encoding.UTF8.GetString(arr);

                        return response.Trim();
                    }
                    catch (WebException exc)
                    { return "Undefined"; }
                }
            }
        }
        catch (Exception ex)
        {
            // TODO: Log trace
            return "Undefined";
        }
    }

Good part is, both sites return IP in plain format. So string operations are avoided.

To check your logic in catch clause, you can reproduce Timeout by hitting a non available port. eg: http://checkip.amazonaws.com:81/

1

The IPIFY API is nice, as it can respond in raw text and JSON. It can also do callbacks etc. The only problem is that it responds in IPv4, not 6.

1

I had almost the same as Jesper, only I reused the webclient and disposed it correctly. Also I cleaned up some responses by removing the extra \n at the end.


    private static IPAddress GetExternalIp () {
      using (WebClient client = new WebClient()) {
        List<String> hosts = new List<String>();
        hosts.Add("https://icanhazip.com");
        hosts.Add("https://api.ipify.org");
        hosts.Add("https://ipinfo.io/ip");
        hosts.Add("https://wtfismyip.com/text");
        hosts.Add("https://checkip.amazonaws.com/");
        hosts.Add("https://bot.whatismyipaddress.com/");
        hosts.Add("https://ipecho.net/plain");
        foreach (String host in hosts) {
          try {
            String ipAdressString = client.DownloadString(host);
            ipAdressString = ipAdressString.Replace("\n", "");
            return IPAddress.Parse(ipAdressString);
          } catch {
          }
        }
      }
      return null;
    }

0
public string GetClientIp() {
    var ipAddress = string.Empty;
    if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null) {
        ipAddress = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
    } else if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"] != null &&
               System.Web.HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"].Length != 0) {
        ipAddress = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"];
    } else if (System.Web.HttpContext.Current.Request.UserHostAddress.Length != 0) {
        ipAddress = System.Web.HttpContext.Current.Request.UserHostName;
    }
    return ipAddress;
} 

works perfect

4
  • Could you add some commentary?
    – Robert
    May 13, 2015 at 8:22
  • 1
    It don't works. ServerVariables member throws a null reference exception. Nov 16, 2015 at 11:57
  • 1
    This seems to be code to find REMOTE client ip, not YOUR client public ip.
    – AFract
    Aug 17, 2017 at 11:56
  • This seems to work only with ASPX, where System.Web.HttpContext.Current is not null. ;)
    – SiL3NC3
    Nov 7, 2023 at 17:09
0
using System.Net;

private string GetWorldIP()
{
    String url = "http://bot.whatismyipaddress.com/";
    String result = null;

    try
    {
        WebClient client = new WebClient();
        result = client.DownloadString(url);
        return result;
    }
    catch (Exception ex) { return "127.0.0.1"; }
}

Used loopback as fallback just so things don't fatally break.

0

WebClient, WebRequest and many other are obsolete, consider using it:

public static IPAddress? GetExternalIP ()
{
    try
    {
        using (var client = new HttpClient())
            return IPAddress.Parse(client.GetAsync("http://ipinfo.io/ip").Result.Content.ReadAsStringAsync().Result);
    }
    catch (Exception ex)
    {
        return null;
    }
}
-1

I worked on https://urlyup.com that will give you a publicly reachable URL to localhost on any port and with just one API call. That will help some with the same issue, let me know if it helps and if you have any questions then.

-3

Or this, it works quite well i think for what i needed. It's from here.

public IPAddress GetExternalIP()
{
    WebClient lol = new WebClient();
    string str = lol.DownloadString("http://www.ip-adress.com/");
    string pattern = "<h2>My IP address is: (.+)</h2>"
    MatchCollection matches1 = Regex.Matches(str, pattern);
    string ip = matches1(0).ToString;
    ip = ip.Remove(0, 21);
    ip = ip.Replace("

    ", "");
    ip = ip.Replace(" ", "");
    return IPAddress.Parse(ip);
}
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.