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> ; 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