I created a class that will be used to deserialize json response from api service from easyjquery.com:
private struct GeoIPResponse { //this is the response I get //{"IP":"127.0.0.1","continentCode":"Unknown","continentName":"Unknown", //"countryCode2":"Unknown","COUNTRY":"Unknown","countryCode3":"Unknown","countryName":"Unknown","regionName":"Unknown", //"cityName":"Unknown","cityLatitude":0,"cityLongitude":0,"countryLatitude":0,"countryLongitude":0,"localTimeZone":"Unknown", //"localTime":"0"} public string IP; public string continentCode; public string continentName; public string countryCode2; public string COUNTRY; public string countryCode3; public string countryName; public string regionName; public string cityName; public string cityLatitude; public string cityLongitude; public string countryLatitude; public string countryLongitude; public string localTimeZone; public string localTime; } /* then I defined some needful methods: */ ///and finally this is the way I used a service from www.easyjquery.com: :/// Returns Client Ip Address /// static public string ClientIpAddress { get { string _clientIPAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (!string.IsNullOrEmpty(_clientIPAddress)) { string[] ipRange = _clientIPAddress.Split(','); _clientIPAddress = ipRange[ipRange.Length - 1]; } else { _clientIPAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } return _clientIPAddress; } } public string _WebRequest(string strURL) { String strResult; WebResponse objResponse; WebRequest objRequest = HttpWebRequest.Create(strURL); objRequest.Method = "GET"; objResponse = objRequest.GetResponse(); using (StreamReader sr = new StreamReader(objResponse.GetResponseStream())) { strResult = sr.ReadToEnd(); sr.Close(); } return strResult; }
protected void Page_Load(object sender, EventArgs e) { txtMessage.Text = ""; string Result = _WebRequest("http://api.easyjquery.com/ips/?ip=" + ClientIpAddress + "&full=true"); txtMessage.Text = Result; JavaScriptSerializer jss = new JavaScriptSerializer(); var clientGeoLocation = jss.Deserialize < GeoIPResponse >(Result); lblMessage.Text = " Your short details: " + "Country: " + clientGeoLocation.COUNTRY + ", CountryCode 2: " + clientGeoLocation.countryCode2 + ", CountryCode 3: " + clientGeoLocation.countryCode3 + ", TimeZone: " + clientGeoLocation.localTimeZone; }
There is error Invalid JSON primitive: .
ReplyDeletein this line:- var clientGeoLocation = jss.Deserialize(Result);
you should write more descriptive error result.
Deletehttp://api.easyjquery.com/ips/ didn't return any response
ReplyDelete