Get public/external IP address?

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

    The IPIFY API is nice, as it can respond in raw text and JSON. It can also do callbacks etc. The only problem is that it responds in IPv4, not 6.

    0 讨论(0)
  • 2020-11-22 14:33

    When I debug, I use following to construct the externally callable URL, but you could just use first 2 lines to get your public IP:

    public static string ExternalAction(this UrlHelper helper, string actionName, string controllerName = null, RouteValueDictionary routeValues = null, string protocol = null)
    {
    #if DEBUG
        var client = new HttpClient();
        var ipAddress = client.GetStringAsync("http://ipecho.net/plain").Result; 
        // above 2 lines should do it..
        var route = UrlHelper.GenerateUrl(null, actionName, controllerName, routeValues, helper.RouteCollection, helper.RequestContext, true); 
        if (route == null)
        {
            return route;
        }
        if (string.IsNullOrEmpty(protocol) && string.IsNullOrEmpty(ipAddress))
        {
            return route;
        }
        var url = HttpContext.Current.Request.Url;
        protocol = !string.IsNullOrWhiteSpace(protocol) ? protocol : Uri.UriSchemeHttp;
        return string.Concat(protocol, Uri.SchemeDelimiter, ipAddress, route);
    #else
        helper.Action(action, null, null, HttpContext.Current.Request.Url.Scheme)
    #endif
    }
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-11-22 14:35

    In theory your router should be able to tell you the public IP address of the network, but the way of doing this will necessarily be inconsistent/non-straightforward, if even possible with some router devices.

    The easiest and still a very reliable method is to send a request to a web page that returns your IP address as the web server sees it. Dyndns.org provides a good service for this:

    http://checkip.dyndns.org/

    What is returned is an extremely simple/short HTML document, containing the text Current IP Address: 157.221.82.39 (fake IP), which is trivial to extract from the HTTP response.

    0 讨论(0)
  • 2020-11-22 14:38

    Using a great similar service

    private string GetPublicIpAddress()
    {
        var request = (HttpWebRequest)WebRequest.Create("http://ifconfig.me");
    
        request.UserAgent = "curl"; // this will tell the server to return the information as if the request was made by the linux "curl" 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", "");
    }
    
    0 讨论(0)
  • 2020-11-22 14:38
    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

    0 讨论(0)
提交回复
热议问题