How to get country name from api codebehind file so i can use it to display in gridview?
foreach (var i in data)
string country = "http://www.geoplugin.net/json.gp?ip=xx.xx.xx.xx"
country=country+"i.ip";
i.Country = country;
Create a class that contains keys from http://www.geoplugin.net/json.gp as String property. The minimum looks like this:
public class GeoPlugin
{
public String geoplugin_countryName { get; set; }
}
Perform a HttpWebRequest and deserialize the JSON response into the class you created and access the country property:
String url = String.Format("http://www.geoplugin.net/json.gp?ip={0}", "173.194.112.31");
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.Method = WebRequestMethods.Http.Get;
httpWebRequest.Accept = "application/json";
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()))
{
JavaScriptSerializer jss = new JavaScriptSerializer();
var result = jss.Deserialize<GeoPlugin>(streamReader.ReadToEnd());
i.Country = result.geoplugin_countryName;
}
To use JavaScriptSerializer
you need to reference System.Web.Extensions
(Version 4.0 ) and not use the .net 4.0 Client Profile.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.