2011-08-10 71 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;