js獲取IP地址方法總結_轉


js代碼獲取IP地址的方法,如何在js中取得客戶端的IP地址。
原文地址:js獲取IP地址的三種方法 http://www.jbxue.com/article/11338.html

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

<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
<script type="text/<A class="infotextkey" href="http://www.jbxue.com/" target=_blank>javascript</A>">
document.write(returnCitySN["cip"]+','+returnCitySN["cname"])
</script>
2,js取得IP地址的方法二
<script language="javascript" type="text/javascript" src="http://fw.qq.com/ipaddress">
</script> 
<script>document.write("你的IP是:"+IPData[0]+",來自:"+IPData[2]);
</script>
3,js取得ip地址的方法三,騰訊IP,轉UTF-8:
<script type="text/javascript" src="http://fw.qq.com/ipaddress" charset="gb2312"></script>
$(document).ready(function() {
$("#ip").val(IPData[0]);
$("#add").val(IPData[2]);
})

參考鏈接:

 

 

/// <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地址

 

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM