The major websites are provided according to the IP to obtain the user's geographical location information, here to Sina's interface as an example
The interface address is: http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=218.18.171.146
Code:
1 #regionGet locations based on IP2 ///Get IP Attribution3 /// </summary>4 /// <param name= "IP" >IP</param>5 /// <returns>Place of Attribution</returns>6 Public Static stringGetipaddress (stringIP)7 {8JavaScriptSerializer Jss =NewJavaScriptSerializer ();9 //Http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=218.18.171.146 call Sina's interfaceTen //var remote_ip_info = {"ret": 1, "Start":-1, "End":-1, "Country": "\u4e2d\u56fd", "Province": "\u5e7f\u4e1c", "City": " \u6df1\u5733 "," District ":" "," ISP ":" "," type ":" "," desc ":"}; One stringAddress =string. Empty; A Try - { - stringRetext = Webrequestpostorget ("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip="+ip,""); theRetext = Retext.split ('=')[1]. Split (';')[0]. Trim (); -dictionary<string,Object> Dictext = (dictionary<string,Object>) Jss.deserializeobject (retext); -Address = dictext[" City"]. ToString (); - } + Catch { } - returnaddress; + } A #endregion
View Code
One of the Webrequestpostorget methods:
1 #regionPost/get Commit Call Fetch2 /// <summary>3 ///Post/get commit call Fetch4 /// </summary>5 /// <param name= "url" >Submit Address</param>6 /// <param name= "param" >Parameters</param>7 /// <returns>string</returns>8 Public Static stringWebrequestpostorget (stringsURL,stringSParam)9 {Ten byte[] bt =System.Text.Encoding.UTF8.GetBytes (sParam); One AUri Uriurl =NewUri (surl); -HttpWebRequest req = (HttpWebRequest) httpwebrequest.create (Uriurl);//HttpWebRequest req = (HttpWebRequest) httpwebrequest.create (url + (URL). IndexOf ("?") >-1? "" : "?") + param); -Req. Method ="Post"; theReq. Timeout = -* +; -Req. ContentType ="application/x-www-form-urlencoded;"; -Req. ContentLength =BT. Length; - + using(Stream Reqstream = req. GetRequestStream ())//Using use can release memory within a using segment - { +Reqstream.write (BT,0, Bt. Length); A Reqstream.flush (); at } - Try - { - using(WebResponse res =req. GetResponse ()) - { - //handle the content of the received page here in -Stream Resstream =Res. GetResponseStream (); to +StreamReader Resstreamreader =NewStreamReader (Resstream, System.Text.Encoding.UTF8); - the stringResline; * $System.Text.StringBuilder Resstringbuilder =NewSystem.Text.StringBuilder ();Panax Notoginseng - while((Resline = Resstreamreader.readline ())! =NULL) the { +Resstringbuilder.append (Resline +System.Environment.NewLine); A } the + resstream.close (); - resstreamreader.close (); $ $ returnresstringbuilder.tostring (); - } - } the Catch(Exception ex) - {Wuyi returnEx. Message;//URL error when return wrong the } - } Wu #endregionPost/get Commit Call Fetch
View Code
Get a user's geographic location based on IP