about how ASP. IP Address for browser access?

Source: Internet
Author: User
Tags servervariables
This article mainly introduces the ASP. NET to obtain the client browser access to the IP address of the instance code, with a certain reference value, interested in small partners can refer to

This article describes the ASP. NET to get the client browser access to the IP address of the instance code, share to everyone, you also leave a note

1. js method


<! DOCTYPE html> 

2. Background Code Implementation


#region IP Address Restrictions 2017-07-18//<summary>////</summary>/<returns></returns&gt    ;      public bool Isipvalidate () {BOOL flag = false;      String userip = Getloginip ();      string[] addr = Getaddressbyip (Userip);      String Addrs = Addr[0] + addr[1]; if ("Beijing"). Equals (addr[0]) | | "Beijing".      Equals (Addr[1])) {flag = true;    } return flag; }///<summary>//Get the IP address of the remote access user///</summary>//<returns> return IP address </returns> PR      otected string Getloginip () {String loginip = "";      request.servervariables[""]--Gets the service variable collection if (request.servervariables["REMOTE_ADDR"]! = NULL)//determines if the IP address of the remote host making the request is empty {//Gets the IP address of the remote host making the request Loginip = request.servervariables["REMOTE_ADDR"].      ToString (); }//Determine if the registered user is using the set proxy else if (request.servervariables["Http_via"]! = null) {if (request.servervariabl         es["Http_x_forwarded_for"]! = null) { Gets the server IP address of the proxy Loginip = request.servervariables["Http_x_forwarded_for"].        ToString ();        } else {//Get client IP Loginip = request.userhostaddress;      }} else {//Get client IP Loginip = request.userhostaddress;    } return Loginip; }///<summary>////</summary> public string[] Getaddressbyip (string IP) {St      Ring PostURL = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=" + IP; string res = Getdatabypost (posturl);//The data returned by this request is: res=1t115.193.210.0t115.194.201.255t China t Zhejiang t Hangzhou T telecom string[] arr = Getar      Eainfolist (RES);    return arr; }//<summary>///POST request data///</summary>//<param name= "url" ></param>//< Returns></returns> public string getdatabypost (string url) {HttpWebRequest req = (HttpWebRequest) webr Equest.      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>///For processing the required data//</summary>//<param name= "IP" ></param>///< returns></returns> public static string[] Getareainfolist (string ipdata) {//1t115.193.210.0t115.194.2      01.255t China t Zhejiang t Hangzhou T Telecom string[] Areaarr = new STRING[10];      string[] Newareaarr = new string[2];        try {//Fetch the desired data, here only the provinces Areaarr = ipdata.split (' t '); NewarEaarr[0] = areaarr[4];//province newareaarr[1] = areaarr[5];//City} catch (Exception e) {} retur    n Newareaarr; } #endregion
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.