Delphi: Retrieve city from public ip address -


how city name http://whatismyipaddress.com/ website?

this attempt:

var   sl: tstringlist;   a: integer;   s: string; begin   sl := tstringlist.create;   try     httpgettext('http://whatismyipaddress.com/', sl);     := pos('your ip address is', sl.text);     if > 0     begin       s := copy(sl.text, a, 40);       := pos('</', s);       s := copy(s, 19, - 19);       ip := s;     end     else       ip := 'unknow ip';       freeandnil(sl);   end; 

is way without use synapse library changing (httpgettext) (idhttp1.get) ?

thanks me!

you have download html content whatismyipaddress.com generates, , parse html needed. can use http client (indy, ics, synapse, curl, wininet/winhttp, raw winsock calls, etc) download html. city appears once in html, inside table, eg:

<tr><th style="font-weight:bold;color:#676769;">city:</th><td style="font-size:14px;">name here</td></tr> 

so substring search of html 'city:' , extract content of subsequent <td> tag.

such sites designed human consumption. @fvu mentioned, should querying webservice freegeoip.net designed machine consumption instead. makes parsing data much easier , predictable. freegeoip.net provides data in several formats - csv, xml, , json, of machine parsable. , delphi has had built-in xml parser years, , modern delphi versions have built-in json parser well. , of course, there third-party parsers readily available.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -