[ASP. NET advanced] returns the corresponding location information based on the IP address, asp. netip
In fact, it is to use the functional interface of Baidu's IP library and then process it as follows:
Preparations:
1. Register as a developer, create an application to obtain the accesskey of Baidu API call, Baidu Development Center address: http://developer.baidu.com/
2. API address: http://developer.baidu.com/map/index.php? Title = webapi/ip-api #. E4.BD. BF. E7.94.A8. E6.96.B9. E6.B3.95
3. Prepare for development. The steps are as follows:
(1) obtain the Client IP Address
/// <Summary> obtain the Client IP address (ignore proxy) </summary> /// <returns> If the proxy fails, return the return address </returns> public static string GetHostAddress () {string userHostAddress = HttpContext. current. request. userHostAddress; if (string. isNullOrEmpty (userHostAddress) {userHostAddress = HttpContext. current. request. serverVariables ["REMOTE_ADDR"];} // Finally, determine whether the obtained IP address is successful and check the IP address format (check whether the format is very important) if (! String. isNullOrEmpty (userHostAddress) & IsIP (userHostAddress) {return userHostAddress;} return "127.0.0.1 ";} /// <summary> /// check the ip address format /// </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 ?) \.) {3} (2 [0-4] \ d | 25 [0-5] | [01]? \ D ?) $ ");
(2) Get the encoding service address
Private const string THE_KEY = "***************"; // your AK key // <summary> returns the UTF-8 code service address </summary> // <returns> service address </returns> public string GetPostUrl (string theIP) {string postUrl = "http://api.map.baidu.com/location/ip? Ak = "+ THE_KEY +" & ip = "+ theIP +" & coor = bd09ll "; return postUrl ;}
(3) Get the address resolution result through the service address
/// <Summary> return result (Address Resolution result) </summary> public static string GetInfoByUrl (string url) {// you only need to pass the assembled URL to the function during the call. String strRet = null if (url = null | url. trim (). toString () = "") {return strRet;} string targeturl = url. trim (). toString (); try {HttpWebRequest hr = (HttpWebRequest) WebRequest. create (targeturl); hr. userAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"; hr. method = "GET"; hr. timeout = 30*60*1000; WebResponse hs = hr. getResponse (); Stream sr = hs. getResponseStream (); StreamReader ser = new StreamReader (sr, Encoding. default); strRet = ser. readToEnd ();} catch (Exception ex) {strRet = null;} return strRet ;}
(4) Finally, simply call the above method and convert the escape characters in the string.
string theIP = YMethod.GetHostAddress(); string msg = YMethod.GetInfoByUrl(GetPostUrl(theIP)); msg = System.Text.RegularExpressions.Regex.Unescape(msg); Response.Write(msg);
Test address: http://ycdoit.com/test/getiptoad.aspx