Get public/external IP address?

前端 未结 26 1977
鱼传尺愫
鱼传尺愫 2020-11-22 14:32

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?

相关标签:
26条回答
  • 2020-11-22 14:46

    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());
    
    0 讨论(0)
  • 2020-11-22 14:47

    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;
        }
    
    0 讨论(0)
  • 2020-11-22 14:48

    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;
    }
    
    0 讨论(0)
  • 2020-11-22 14:51

    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 WebClient())
                foreach (var service in services)
                {
                    try { return IPAddress.Parse(webclient.DownloadString(service)); } catch { }
                }
            return null;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 14:52

    Using C#, With webclient its a short one.

    public static void Main(string[] args)
    {
        string externalip = new WebClient().DownloadString("http://icanhazip.com");            
        Console.WriteLine(externalip);
    }
    

    Command Line (works on both Linux and Windows)

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

    OR

    curl http://ipinfo.io/ip
    
    0 讨论(0)
  • 2020-11-22 14:52

    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;
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题