js代码获取IP地址的方法,如何在js中取得客户端的IP地址。
原文地址:js获取IP地址的三种方法 http://www.jbxue.com/article/11338.html

1,js取得IP地址的方法一

})

参考链接:

 

 

/// <summary>
        /// 获取客户端IP地址(无视代理)
        /// </summary>
        /// <returns>若失败则返回回送地址</returns>
        public static string GetHostAddress()
        {
            string userHostAddress = string.Empty;

            WebRequest wr = WebRequest.Create("http://pv.sohu.com/cityjson?ie=utf-8");
            Stream s = wr.GetResponse().GetResponseStream();
            StreamReader sr = new StreamReader(s, System.Text.Encoding.UTF8);
            string all = sr.ReadToEnd(); //读取网站的数据

            int start = all.IndexOf(": \"") + 3;
            int end = all.IndexOf("\",", start);
            userHostAddress = all.Substring(start, end - start);

            sr.Close();
            s.Close();

            //最后判断获取是否成功,并检查IP地址的格式(检查其格式非常重要)
            if (!string.IsNullOrEmpty(userHostAddress) && IsIP(userHostAddress))
            {
                return userHostAddress;
            }
            return "127.0.0.1";
        }

        /// <summary>
        /// 检查IP地址格式
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static bool IsIP(string ip)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
        }
获取客户端IP地址

相关文章: