chu*_*byk 11 delphi ip external
我需要从Delphi获取我的外部(公共)IP地址.
例如,www.whatismyip.com显示的相同IP .
我怎样才能做到这一点 ?Winsock不允许这样做.
我认为你不能.好吧,你可以拨打一些服务,告诉你你的IP地址是什么,(例如:http://www.whatismyip.com/)并从响应中找出来.但我不认为你的电脑上的任何东西都能告诉你你的IP地址对外界的影响.
未经测试,但我认为你可以用Indy做到这一点:
MyPublicIP := IdHTTP1.Get('http://automation.whatismyip.com/n09230945.asp');
Run Code Online (Sandbox Code Playgroud)
在使用此之前,请查看规则/政策:http://www.whatismyip.com/faq/automation.asp.
您可以使用以下网站:http://ipinfo.io/json.它以JSON格式返回有关您当前Internet连接的信息.
在delphi中,您需要使用IdHTTP这种方式:IdHTTP1.Get('http://ipinfo.io/json')
它将返回包含所有数据的字符串.您可以使用JSON您喜欢的解释器,也可以使用lkJSON以下示例:
json := TlkJSON.ParseText(MainEstrutura.IdHTTP1.Get('http://ipinfo.io/json')) as TlkJSONobject;
str := json.Field['ip'].Value;
Run Code Online (Sandbox Code Playgroud)
我希望能帮助你.
这对我有用:
uses JSON,IdHTTP;
function GetIP():String;
var LJsonObj : TJSONObject;
str:string;
http : TIdHttp;
begin
str:='';
http:=TIdHTTP.Create(nil);
try
str:=http.Get('http://ipinfo.io/json');
LJsonObj:= TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(str),0) as TJSONObject;
str := LJsonObj.Get('ip').JsonValue.Value;
LJsonObj.Free;
http.Free;
Except
end;
result:=str;
end;
Run Code Online (Sandbox Code Playgroud)