回答
我不認爲你可以。那麼,你可以撥打一些服務來告訴你你的IP地址是什麼(例如:http://www.whatismyip.com/),並從響應中找出答案。但是我不認爲你的電腦上的任何東西都能告訴你你的IP地址是什麼樣子的,對外界來說。
未經檢驗的,但我認爲你可以用印地做到這一點:使用本品前http://www.whatismyip.com/faq/automation.asp:
MyPublicIP := IdHTTP1.Get('http://automation.whatismyip.com/n09230945.asp');
請仔細閱讀在規則/政策。
...原因是您的公共地址不一定附加到您的計算機上。很多時候,它被分配到一個外部路由器,這是一個完全不同的計算機對所有影響。 –
是的。公共IP和適配器與默認網關是兩回事。 –
從內存,未經測試:
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;
你可以使用這個網站: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;
我希望你的幫助。
如果使用'http:// ipinfo.io/ip'代替它,它將以純文本格式返回IP本身,而不會將其包裝在必須解析的JSON中。 –
作爲這個答案中的代碼,你需要使用:'http:// ipinfo.io/json'來獲取關於這個IP的所有信息。 –
OP沒有要求檢索關於IP的所有細節,只需要檢索IP本身。所以'/ json'在'/ ip'就足夠了的時候就過分了。如果您閱讀網站的文檔,可以單獨檢索各個字段。 –
這個工作對我來說:
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;
我更喜歡使用將自己的IP報告爲純文本而不需要任何額外元數據格式的服務。例如:'function GetIP:String;開始於TIdHTTP.Create嘗試結果:= http。獲取( 'http://ipinfo.io/ip');終於免費;結束;結束;' –
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;
- 1. 在Golang獲取公共IP
- 2. Symfony2 - 如何獲得外部(公共)和內部(本地)IP?
- 3. 如何獲得公共IP
- 4. 在javascript中獲取公共ip地址
- 5. 遠程獲取公共IP
- 6. 獲取外部IP
- 7. 如何使用公共IP地址在外部從外部託管網站?
- 8. 如何在Ubuntu中獲取公共IP地址
- 9. 如何在python中獲取當前ec2實例的公共ip?
- 10. 如何在php中獲取客戶端的公共ip
- 11. 如何在C#中獲取用戶的公共IP地址
- 12. 如何在公共IP
- 13. 在Java中獲取'外部'IP地址
- 14. 獲取路由器的公共ip
- 15. 獲取公共(遠程)IP地址
- 16. 通過API獲取我的公共IP
- 17. 如何從kubernetes中的容器獲取公共IP
- 18. Android獲取外部IP
- 19. 從部署的應用程序獲取Azure公共IP地址
- 20. 如何在目標c中獲取外部IP
- 21. 如何在局域網中獲取外部IP地址?
- 22. 如何在Python中獲取套接字的外部IP?
- 23. 如何在C++中獲取外部IP地址?
- 24. 如何在Symfony2控制器中獲取外部IP地址?
- 25. 如何從XMPP綁定消息獲取我的公共IP?
- 26. 如何獲取Windows Azure的公共靜態IP
- 27. AWS Cloud Formation - 如何獲取公共ip地址的ec2實例
- 28. 如何使用Azure SDK獲取Azure VM的公共IP
- 29. 在Logstash中,如何獲取客戶端的公共IP地址作爲字段
- 30. 什麼是AWS公共IP限制? (公共IP不彈性IP)
通過外部IP地址,你的意思是你的互聯網訪問的IP地址?或者你的意思是一個不是127.0.0.1的本地網絡地址? – Tremmors
互聯網訪問地址,與www.whatismyip.com所示的相同 – chubbyk
此問題顯示您的計算機的實際IP地址; http://stackoverflow.com/questions/576538/delphi-how-to-get-all-local-ips –