问题
I am getting the user IP address using the below code
function GetIp: string;
var
WinHttpReq: Variant;
begin
try
WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
WinHttpReq.Open('GET', 'http://ipinfo.io/ip', False);
WinHttpReq.Send;
Result := Trim(WinHttpReq.ResponseText);
except
Log(GetExceptionMessage);
Result := '8.8.8.8';
end;
end;
After getting the users IP address I need to check if that IP address already exists in my online JSON list.
Thanks
回答1:
The simplest solution is to download your JSON text file and search your IP address.
Reuse your code to retrieve a document using HTTP (or better HTTPS):
function HttpGet(Url: string): string;
var
WinHttpReq: Variant;
begin
WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
WinHttpReq.Open('GET', Url, False);
WinHttpReq.Send;
Result := Trim(WinHttpReq.ResponseText);
end;
And then you can use it like:
var
Ip: string;
List: string;
begin
try
Ip := HttpGet('https://ipinfo.io/ip');
List := HttpGet('https://www.example.com/publicly/available/list.json');
if Pos('["' + Ip + '"]', List) > 0 then
begin
Log(Format('IP %s is in the list', [Ip]));
end
else
begin
Log(Format('IP %s is not in the list', [Ip]));
end;
except
Log(Format('Error testing if IP is in the list - %s', [GetExceptionMessage]));
end;
end;
Though you will have to make your list publicly available. Currently your URL cannot be accessed, without logging to Google first.
If you want to process your JSON properly, see
How to parse a JSON string in Inno Setup?
来源:https://stackoverflow.com/questions/62330068/check-an-ip-address-against-an-online-list-in-inno-setup