c# - How to find country from ip adress? -


i using this api find country of user. able find country on web page in xml format. here can see xml file example. problem can not read xml in c# code. here code

string userip = request.servervariables["remote_addr"].tostring(); string apikey = "5d3d0cdbc95df34b9db4a7b4fb754e738bce4ac914ca8909ace8d3ece39cee3b"; string url = "http://api.ipinfodb.com/v3/ip-country/?key=" + apikey + "&ip=" + userip; xdocument xml = xdocument.load(url); 

but code returns following exception on loading xml.

system.xml.xmlexception: data @ root level invalid. line 1, position 1.

please describe exact method read xml.

i'll isn't xml string subdivided ;:

by giving impossible ip address can see it's composed:

ok;;74.125.45.100;us;united states error;invalid ip address.;127.0.0.1.1;;  ok/error if error, complete error message ip address abbreviation of country country name 

this code should do:

string userip = "127.0.0.1"; string apikey = "5d3d0cdbc95df34b9db4a7b4fb754e738bce4ac914ca8909ace8d3ece39cee3b"; string url = "http://api.ipinfodb.com/v3/ip-country/?key=" + apikey + "&ip=" + userip;  webrequest request = webrequest.create(url);  using (var response = (httpwebresponse)request.getresponse()) {     // try use "correct" charset     encoding encoding = response.characterset != null ? encoding.getencoding(response.characterset) : null;      using (var sr = encoding != null ? new streamreader(response.getresponsestream(), encoding) :                                        new streamreader(response.getresponsestream(), true))     {         var response2 = sr.readtoend();         var parts = response2.split(';');          if (parts.length != 5)         {             throw new exception();         }          string okerror = parts[0];         string message = parts[1];         string ip = parts[2];         string code = parts[3];         string country = parts[4];     } } 

Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -