zoukankan      html  css  js  c++  java
  • 获取远程服务器的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 从数据库中查询地区名称即可
    //记得下载我的数据库哦

  • 相关阅读:
    webpack学习笔记五
    webpack的学习使用四
    判断数组B是否为数组A的子集
    html5标签知多少
    图代文时隐藏文字的兼容实现
    负margin的移位参考线
    font-size 兼容问题
    IE6读取不到样式文件bug
    一个重构眼中的“项目管理”
    唯物 VS 唯心
  • 原文地址:https://www.cnblogs.com/bbxie/p/577920.html
Copyright © 2011-2022 走看看