获取请求页面内网ip外网ip

    /// <summary>
    /// 获取内网IP
    /// </summary>
    /// <returns></returns>
    public static string GetInternalIP()
    {
        IPHostEntry host;
        string localIP = "?";
        host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (IPAddress ip in host.AddressList)
        {
            if (ip.AddressFamily.ToString() == "InterNetwork")
            {
                localIP = ip.ToString();
                break;
            }
        }
        return localIP;
    }

    /// <summary>
    /// 获取外网IP
    /// </summary>
    /// <returns></returns>
    public static string GetExternalIP()
    {
        string result = String.Empty;
        result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; //使用代理服务器
        if (null == result || result == String.Empty)
        {
            result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        }
        if (null == result || result == String.Empty)
        {
            result = HttpContext.Current.Request.UserHostAddress;
        }
        return result;
    }

 

posted @ 2020-07-10 17:08  馒头什么的最喜欢了  阅读(301)  评论(0编辑  收藏  举报