public Dictionary<string, string> IP2Location(string ip)
        {
            //build request
            string req = string.Format(@"http://freegeoip.appspot.com/xml/{0}", ip);

            //send request
            WebResponseHelper resp = new WebRequestHelper(req, WebRequestType.GET).Send();

            //sample response
            /*
            <?xml version="1.0" encoding="UTF-8" ?>
            <Response>
                  <Status>true</Status>
                  <Ip>83.99.162.17</Ip>
                  <CountryCode>LV</CountryCode>
                  <CountryName>Latvia</CountryName>
                  <RegionCode>25</RegionCode>
                  <RegionName>Riga</RegionName>
                  <City>Riga</City>
                  <ZipCode />
                  <Latitude>56.95</Latitude>
                  <Longitude>24.1</Longitude>
            </Response>
            */

            //parse response
            XmlDocument doc = resp.ResponseXml;
            XmlNode rootNode = doc.SelectSingleNode(@"Response");
            Dictionary<string, string> respFields = new Dictionary<string, string>();
            foreach (XmlNode node in rootNode.SelectNodes(@"*"))
            {
                if (!string.IsNullOrEmpty(node.InnerText))
                {
                    respFields.Add(node.Name, node.InnerText);
                }
            }

            //check
            if (respFields.ValueOrDefault("Status") != "true")
            {
                //if failed, set to null
                respFields = null;
            }
            else
            {
                //remove not informative fields
                respFields.Remove(@"Status");
            }

            //return result
            return respFields;
        }
Example #2
0
 public WebResponseHelper(WebRequestHelper request)
 {
     Init(request, null);
 }
Example #3
0
 public WebResponseHelper(WebRequestHelper request, SetRequestAttributesDelegate srd)
 {
     Init(request, srd);
 }
Example #4
0
 private void Init(WebRequestHelper request, SetRequestAttributesDelegate srd)
 {
     _valuePairSeparator = '&';
     _valuePairEqualitySign = '=';
     _request = request;
     _srd = srd;
     if (request.Type == WebRequestType.GET)
     {
         Send();
     }
     else //if (request.Type == WebRequestType.POST)
     {
         Post();
     }
 }