Get public/external IP address?

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

提交回复
热议问题