type
ApiResults =
record
Status: UTF8String;
Country: UTF8String;
CountryCode: UTF8String;
RegionState: UTF8String;
RegionName: UTF8String;
City: UTF8String;
Zip: UTF8String;
Latitude: UTF8String;
Longitude: UTF8String;
CityTimeZone: UTF8String;
ISP: UTF8String;
Organization: UTF8String;
NumberName: UTF8String;
DNS: UTF8String;
MobileConnection: UTF8String;
ProxyConnection: UTF8String;
QueryIP: UTF8String;
ErrorMessage: UTF8String;
end;
function GetUrlContentData(
const Url:
string): UTF8String;
var
NetHandle: HINTERNET;
UrlHandle: HINTERNET;
Buffer:
array[0..1023]
of byte;
BytesRead: dWord;
StrBuffer: UTF8String;
begin
Result := '
';
NetHandle := InternetOpen('
Delphi 2009', INTERNET_OPEN_TYPE_PRECONFIG,
nil,
nil, 0);
if Assigned(NetHandle)
then
try
UrlHandle := InternetOpenUrl(NetHandle, PChar(
Url),
nil, 0, INTERNET_FLAG_RELOAD, 0);
if Assigned(UrlHandle)
then
try
repeat
InternetReadFile(UrlHandle, @Buffer, SizeOf(Buffer), BytesRead);
SetString(StrBuffer, PAnsiChar(@Buffer[0]), BytesRead);
Result := Result + StrBuffer;
until BytesRead = 0;
finally
InternetCloseHandle(UrlHandle);
end
else
raise Exception.CreateFmt('
Cannot open URL %s', [
Url]);
finally
InternetCloseHandle(NetHandle);
end
else
raise Exception.Create('
Unable to initialize Wininet');
end;
function GetUrlContent(
const Url:
string): ApiResults;
begin
Result.Status := GetUrlContentData(
Url+'
?fields=status');
if Pos('
fail', LowerCase(Result.Status)) > 0
then
begin
Result.ErrorMessage := GetUrlContentData(
Url+'
?fields=message');
Result.QueryIP := GetUrlContentData(
Url+'
?fields=query');
end
else
begin
Result.Country := GetUrlContentData(
Url+'
?fields=country');
Result.CountryCode := GetUrlContentData(
Url+'
?fields=countryCode');
Result.RegionState := GetUrlContentData(
Url+'
?fields=region');
Result.RegionName := GetUrlContentData(
Url+'
?fields=regionName');
Result.City := GetUrlContentData(
Url+'
?fields=city');
Result.Zip := GetUrlContentData(
Url+'
?fields=zip');
Result.Latitude := GetUrlContentData(
Url+'
?fields=lat');
Result.Longitude := GetUrlContentData(
Url+'
?fields=lon');
Result.CityTimeZone := GetUrlContentData(
Url+'
?fields=timezone');
Result.ISP := GetUrlContentData(
Url+'
?fields=isp');
Result.Organization := GetUrlContentData(
Url+'
?fields=org');
Result.NumberName := GetUrlContentData(
Url+'
?fields=as');
Result.DNS := GetUrlContentData(
Url+'
?fields=reverse');
Result.MobileConnection := GetUrlContentData(
Url+'
?fields=mobile');
Result.ProxyConnection := GetUrlContentData(
Url+'
?fields=proxy');
Result.QueryIP := GetUrlContentData(
Url+'
?fields=query');
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
ThisLines: ApiResults;
begin
ThisLines := GetUrlContent('
http://ip-api.com/line/' + '
ip-api.com');
Memo1.Lines.Add('
Status: '+ThisLines.Status);
if Pos('
fail', LowerCase(ThisLines.Status)) > 0
then
begin
Memo1.Lines.Add('
ErrorMessage: '+ThisLines.ErrorMessage);
Memo1.Lines.Add('
QueryIP: '+ThisLines.QueryIP);
end
else
begin
Memo1.Lines.Add('
Country: '+ThisLines.Country);
Memo1.Lines.Add('
CountryCode: '+ThisLines.CountryCode);
Memo1.Lines.Add('
RegionState: '+ThisLines.RegionState);
Memo1.Lines.Add('
RegionName: '+ThisLines.RegionName);
Memo1.Lines.Add('
City: '+ThisLines.City);
Memo1.Lines.Add('
Zip: '+ThisLines.Zip);
Memo1.Lines.Add('
Latitude: '+ThisLines.Latitude);
Memo1.Lines.Add('
Longitude: '+ThisLines.Longitude);
Memo1.Lines.Add('
CityTimeZone: '+ThisLines.CityTimeZone);
Memo1.Lines.Add('
ISP: '+ThisLines.ISP);
Memo1.Lines.Add('
Organization: '+ThisLines.Organization);
Memo1.Lines.Add('
NumberName: '+ThisLines.NumberName);
Memo1.Lines.Add('
DNS: '+ThisLines.DNS);
Memo1.Lines.Add('
MobileConnection: '+ThisLines.MobileConnection);
Memo1.Lines.Add('
ProxyConnection: '+ThisLines.ProxyConnection);
Memo1.Lines.Add('
QueryIP: '+ThisLines.QueryIP);
end;
end;