Wednesday, 29 August 2007

Delphi code to fetch your own public IP address

function whatismyip(): String;
var anurl: String;
var r:TregExpr;
var myip: string;
begin
anurl := IdHTTP1.Get('http:\\www.whatismyip.com');
r:= TregExpr.Create;
r.Expression := '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
if r.Exec(anurl) then myip := r.Match[0];
r.Destroy;
whatismyip:=myip;
end;

This snippet requires indy and also regular expressions from http://regexpstudio.com/

There is a version using windows sockets here :-
http://www.delphifusion.com/forum/showthread.php?t=247

5 comments:

Anonymous said...

Thanks for writing this.

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.