13

I need to get my external (public) IP address from Delphi.

The same IP that is shown by www.whatismyip.com for example.

How can I do that ? Winsock doesn't allow this.

kobik
  • 21,001
  • 4
  • 61
  • 121
chubbyk
  • 6,212
  • 13
  • 53
  • 67

5 Answers5

10

You can use this website: http://ipinfo.io/json. It returns the information about your current internet connection in JSON format.

In delphi you need use IdHTTP this way: IdHTTP1.Get('http://ipinfo.io/json') and it will returns a string with all the data. You can use a JSONinterpreter you like or you can use the lkJSON as the following example:

json := TlkJSON.ParseText(MainEstrutura.IdHTTP1.Get('http://ipinfo.io/json')) as TlkJSONobject;

str := json.Field['ip'].Value;

I hope help you.

pedro.olimpio
  • 1,478
  • 2
  • 22
  • 43
  • 8
    If you use `http://ipinfo.io/ip` instead, it returns just the IP by itself in plain text without wrapping it in JSON that has to be parsed. – Remy Lebeau Oct 15 '15 at 00:28
  • As the code in this answer you need use: `http://ipinfo.io/json` to get all information about this IP – pedro.olimpio Oct 15 '15 at 14:01
  • 2
    The OP did not ask to retrieve all the details about the IP, only to retrieve the IP itself. So `/json` is overkill when `/ip` will suffice. If you read the website's documentation, individual fields can be retrieved by themselves. – Remy Lebeau Oct 15 '15 at 20:18
7

I don't think you can. Well, you could call some service that tells you what your IP address appears to be, ( ex: http://www.whatismyip.com/ ) and figure it out from the response. But I don't think anything on your PC will be able to tell you what your IP address looks like, to the outside world.

Untested, but I think you can do this with Indy:

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

Please review the rules/policy at: http://www.whatismyip.com/faq/automation.asp before using this.

Chris Thornton
  • 15,620
  • 5
  • 37
  • 62
  • 12
    ... and the reason is that your public address is not necessarily attached to your computer. Pretty often, it's assigned to an external router, which is an entirely different computer to all effects. – Álvaro González Aug 10 '11 at 18:10
  • Yes. Public IP and adapter with default gateway are two different things. – Marco van de Voort Aug 12 '11 at 09:03
6

this works for me:

  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;
Salim Lachdhaf
  • 1,073
  • 1
  • 9
  • 21
  • 4
    I prefer to use services that report the IP by itself as plain text without any extra metadata formatting. For example: `function GetIP: String; begin with TIdHTTP.Create do try Result := http.Get('http://ipinfo.io/ip'); finally Free; end; end;` – Remy Lebeau Oct 15 '15 at 00:27
  • @RemyLebeau The best and simple solution ! (but replace `http.Get` with `Get`) – Marus Gradinaru Jul 20 '20 at 13:49
2

From memory, untested:

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;
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
0
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;
The North Star
  • 104
  • 1
  • 10