Get public/external IP address?

2019-01-01 02:05发布

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?

24条回答
琉璃瓶的回忆
2楼-- · 2019-01-01 02:36

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");
查看更多
旧时光的记忆
3楼-- · 2019-01-01 02:37

From C#, you could use the web client libraries to fetch whatismyip

查看更多
萌妹纸的霸气范
4楼-- · 2019-01-01 02:39

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;
}
查看更多
流年柔荑漫光年
5楼-- · 2019-01-01 02:40

Similar service

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

            request.UserAgent = "curl"; // this simulate curl linux 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", "");
        }
查看更多
何处买醉
6楼-- · 2019-01-01 02:41
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

查看更多
时光乱了年华
7楼-- · 2019-01-01 02:43
public static string GetPublicIP()
{
    return new System.Net.WebClient().DownloadString("https://ipinfo.io/ip").Replace("\n","");
}
查看更多
登录 后发表回答