關於asp.net如何擷取瀏覽器訪問的IP地址?

來源:互聯網
上載者:User
本篇文章主要介紹了asp.net 擷取用戶端瀏覽器訪問的IP地址的執行個體代碼,具有一定的參考價值,感興趣的小夥伴們可以參考一下

本文介紹了asp.net 擷取用戶端瀏覽器訪問的IP地址的執行個體代碼,分享給大家,自己也留個筆記

1、js方法


<!DOCTYPE html> <html> <head>   <meta charset="UTF-8" />   <title>Document</title>   <script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>    <script type="text/javascript">      document.write('IP地址:' + returnCitySN["cip"] + ', 城市編碼:' + returnCitySN["cid"] + ', 地區:' + returnCitySN["cname"]);   </script> </head> <body>  </body> </html>

2、後台代碼實現


#region IP地址限制功能 2017-07-18      /// <summary>    ///     /// </summary>    /// <returns></returns>    public bool IsIPValidate()    {      bool flag = false;      string userip = GetLoginIp();      string[] addr = GetAddressByIp(userip);      string addrs = addr[0] + addr[1];      if ("北京".Equals(addr[0]) || "北京".Equals(addr[1]))      {        flag = true;      }      return flag;    }      /// <summary>    /// 擷取遠端存取使用者的Ip地址    /// </summary>    /// <returns>返回Ip地址</returns>    protected string GetLoginIp()    {      string loginip = "";      //Request.ServerVariables[""]--擷取服務變數集合       if (Request.ServerVariables["REMOTE_ADDR"] != null) //判斷髮出請求的遠程主機的ip地址是否為空白      {        //擷取發出請求的遠程主機的Ip地址        loginip = Request.ServerVariables["REMOTE_ADDR"].ToString();      }      //判斷登記使用者是否使用設定代理      else if (Request.ServerVariables["HTTP_VIA"] != null)      {        if (Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)        {          //擷取代理的伺服器Ip地址          loginip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();        }        else        {          //擷取用戶端IP          loginip = Request.UserHostAddress;        }      }      else      {        //擷取用戶端IP        loginip = Request.UserHostAddress;      }      return loginip;    }    /// <summary>    /// 根據IP擷取省市    /// </summary>    public string[] GetAddressByIp(string ip)    {      string PostUrl = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=" + ip;      string res = GetDataByPost(PostUrl);//該條請求返回的資料為:res=1t115.193.210.0t115.194.201.255t中國t浙江t杭州t電信      string[] arr = getAreaInfoList(res);      return arr;    }    /// <summary>    /// Post請求資料    /// </summary>    /// <param name="url"></param>    /// <returns></returns>    public string GetDataByPost(string url)    {      HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);      string s = "anything";      byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(s);      req.Method = "POST";      req.ContentType = "application/x-www-form-urlencoded";      req.ContentLength = requestBytes.Length;      Stream requestStream = req.GetRequestStream();      requestStream.Write(requestBytes, 0, requestBytes.Length);      requestStream.Close();        HttpWebResponse res = (HttpWebResponse)req.GetResponse();      StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);      string backstr = sr.ReadToEnd();      sr.Close();      res.Close();      return backstr;    }      /// <summary>    /// 處理所要的資料    /// </summary>    /// <param name="ip"></param>    /// <returns></returns>    public static string[] getAreaInfoList(string ipData)    {      //1t115.193.210.0t115.194.201.255t中國t浙江t杭州t電信      string[] areaArr = new string[10];      string[] newAreaArr = new string[2];      try      {        //取所要的資料,這裡只取省市        areaArr = ipData.Split('t');        newAreaArr[0] = areaArr[4];//省        newAreaArr[1] = areaArr[5];//市      }      catch (Exception e)      {        }      return newAreaArr;    }      #endregion
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.