Asp.Net获取客户端IP地址及地理位置

/// <summary>
        /// 获取IP地址(无视代理)
        /// </summary>
        /// <returns></returns>
        private string getIp()
        {
            if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
            {
                return System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(new char[] { ',' })[0];
            }
            else
            {
                return System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
        }
/// <summary>
        /// 获取IP v4地址
        /// </summary>
        /// <returns></returns>
        public string GetClientIPv4()
        {
            string ipv4 = String.Empty;
            foreach (IPAddress ip in Dns.GetHostAddresses(getIp()))
            {
                if (ip.AddressFamily.ToString() == "InterNetwork")
                {
                    ipv4 = ip.ToString();
                    break;
                }
            }
 
            if (ipv4 != String.Empty)
            {
                return ipv4;
            }
 
            foreach (IPAddress ip in Dns.GetHostEntry(getIp()).AddressList)
            {
                if (ip.AddressFamily.ToString() == "InterNetwork")
                {
                    ipv4 = ip.ToString();
                    break;
                }
            }
            return ipv4;
        }
/// <summary>
        /// 通过IP地址查询网站获取Ip地址
        /// </summary>
        /// <param name="ipAddress"></param>
        /// <returns></returns>
        public string IpLocation(string ipAddress)
        {
            if (string.IsNullOrEmpty(ipAddress.Trim()))
            {
                return null;
            }
            string url = "http://www.ip138.com/ips1388.asp?ip=" + ipAddress + "&action=2";
            string response = GetHttpData(url);
            return GetData(response);
        }
/// <summary>
        /// 获取整体页面数据
        /// </summary>
        /// <param name="Url"></param>
        /// <returns></returns>
        public string GetHttpData(string Url)
        {
            string sException = null;
            string sRslt = null;
            WebResponse oWebRps = null;
            WebRequest oWebRqst = WebRequest.Create(Url);
            oWebRqst.Timeout = 50000;
            try
            {
                oWebRps = oWebRqst.GetResponse();
            }
            catch (WebException e)
            {
                sException = e.Message.ToString();
                Response.Write(sException);
            }
            catch (Exception e)
            {
                sException = e.ToString();
                Response.Write(sException);
            }
            finally
            {
                if (oWebRps != null)
                {
                    StreamReader oStreamRd = new StreamReader(oWebRps.GetResponseStream(), Encoding.GetEncoding("GB2312"));
                    sRslt = oStreamRd.ReadToEnd();
                    oStreamRd.Close();
                    oWebRps.Close();
                }
            }
            return sRslt;
        }
/// <summary>
        /// 获取页面中指定内容
        /// </summary>
        /// <param name="Html"></param>
        /// <returns></returns>
        public string GetData(string Html)
        {
            string Pat = @"<li>本站主数据:(?<location>[^<>]+?)</li>";
            Regex Re = new Regex(Pat);
            Match Ma = Re.Match(Html);
            if (Ma.Success)
            {
                return Ma.Groups["location"].Value.Trim();
            }
            else
            {
                return "无";
            }             
        }

posted on 2014-04-22 08:13  公爵大人  阅读(590)  评论(0)    收藏  举报

导航