Get public/external IP address?

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

    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;
    }
    

提交回复
热议问题