Get public/external IP address?

前端 未结 26 1989
鱼传尺愫
鱼传尺愫 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:42

    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*:

    • https://ipinfo.io/ip/
    • https://api.ipify.org/
    • https://icanhazip.com/
    • https://checkip.amazonaws.com/
    • https://wtfismyip.com/text

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

提交回复
热议问题