Get public/external IP address?

前端 未结 26 2312
鱼传尺愫
鱼传尺愫 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: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;
    }
    

提交回复
热议问题