获取远程服务器的ip地址以及地区地址

采用Request.ServerCariables[]方法,先获得Ip地址,方法如下
            private string VisitedIP;
            if (Request.ServerVariables["HTTP_VIA"] != null)
            {
                VisitedIP = Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
            }
            else
            {
                VisitedIP = Request.ServerVariables["REMOTE_ADDR"].ToString();
            }

    /*
如果客户端使用了代理服务器,使用Request.ServerVariables("HTTP_X_FORWARDED_FOR") 得到IP地址,如果没用使用代理服务器,得到的是"",则用Request.ServerVariables("REMOTE_ADDR") 得到IP地址. 

*/
 StringBuilder strResult = new StringBuilder();
        string[] strip = VisitedIP.Split('.');//根据需要,必须将ip地址转换成标准格式的ip地址如:024.042.000.000
        foreach (string strips in strip)
        {
            strResult.Append(strips.PadLeft(3, '0'));
            strResult.Append(".");
        }
        string finalip = strResult.ToString().Substring(0, 15).Trim();

//根据获得的finalip 从数据库中查询地区名称即可
//记得下载我的数据库哦

原文地址:https://www.cnblogs.com/bbxie/p/577920.html