2011-08-10 55 views
10

我需要从Delphi获取我的外部(公共)IP地址。如何在Delphi中获取外部(公共)IP

例如,www.whatismyip.com所示的IP地址相同。

我该怎么做? Winsock不允许这样做。

+0

通过外部IP地址,你的意思是你的互联网访问的IP地址?或者你的意思是一个不是127.0.0.1的本地网络地址? – Tremmors

+0

互联网访问地址,与www.whatismyip.com所示的相同 – chubbyk

+0

此问题显示您的计算机的实际IP地址; http://stackoverflow.com/questions/576538/delphi-how-to-get-all-local-ips –

回答

7

我不认为你可以。那么,你可以拨打一些服务来告诉你你的IP地址是什么(例如:http://www.whatismyip.com/),并从响应中找出答案。但是我不认为你的电脑上的任何东西都能告诉你你的IP地址是什么样子的,对外界来说。

未经检验的,但我认为你可以用印地做到这一点:使用本品前http://www.whatismyip.com/faq/automation.asp

MyPublicIP := IdHTTP1.Get('http://automation.whatismyip.com/n09230945.asp'); 

请仔细阅读在规则/政策。

+12

...原因是您的公共地址不一定附加到您的计算机上。很多时候,它被分配到一个外部路由器,这是一个完全不同的计算机对所有影响。 –

+0

是的。公共IP和适配器与默认网关是两回事。 –

1

从内存,未经测试:

function GetMyHostAddress: string; 
var 
    http: IWinHttpRequest; 
begin 
    http := CreateOleObject('WinHttp.WinHttpRequest5.1') as IWinHttpRequest; 
    http.Open('GET', 'http://automation.whatismyip.com/n09230945.asp', False); 
    http.Send(EmptyParam); 

    if http.StatusCode = 200 then 
     Result := http.ResponseText 
    else 
     Result := ''; 
end; 
6

你可以使用这个网站:http://ipinfo.io/json。它会以JSON格式返回有关您当前的互联网连接的信息。

在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; 

我希望你的帮助。

+4

如果使用'http:// ipinfo.io/ip'代替它,它将以纯文本格式返回IP本身,而不会将其包装在必须解析的JSON中。 –

+0

作为这个答案中的代码,你需要使用:'http:// ipinfo.io/json'来获取关于这个IP的所有信息。 –

+2

OP没有要求检索关于IP的所有细节,只需要检索IP本身。所以'/ json'在'/ ip'就足够了的时候就过分了。如果您阅读网站的文档,可以单独检索各个字段。 –

1

这个工作对我来说:

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; 
+2

我更喜欢使用将自己的IP报告为纯文本而不需要任何额外元数据格式的服务。例如:'function GetIP:String;开始于TIdHTTP.Create尝试结果:= http。获取( 'http://ipinfo.io/ip');终于免费;结束;结束;' –

-1
Function GetMyIP:string; 
var 
    xmlhttp:olevariant; 
    s,p:integer; 
    temp:string; 
begin 
    result:=emptystr; 
    xmlhttp:=CreateOleObject('Microsoft.XMLHTTP'); 
    try 
    xmlhttp.open('GET', 'http://www.findipinfo.com/', false); 
    xmlhttp.SetRequestHeader('User-Agent','Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3'); 
    xmlhttp.send(null); 
    except 
    exit; 
    end; 
    if(xmlhttp.status = 200) then 
    temp:=trim(VarToStr(xmlhttp.responseText)); 
    xmlhttp:=Unassigned; 
    s:=pos('Address Is:',temp); 
    if s>0 then 
    inc(s,11) 
    else 
    exit; 
    temp:=copy(temp,s,30); 
    s:=pos('<',temp); 
    if s=0 then exit 
    else 
    dec(s); 
    result:=trim(copy(temp,1,s)); 
end;