Recent

Author Topic: Get IP from website url  (Read 3055 times)

Ericktux

  • Sr. Member
  • ****
  • Posts: 345
Get IP from website url
« on: August 09, 2021, 05:59:40 am »
hello friends, how to get the ip of a certain url, for example:
of
www.google.com

i get
172.217.192.104

I do this by "ping" with cmd, but there will be some code that will facilitate this, thanks

rsz

  • New Member
  • *
  • Posts: 45
Re: Get IP from website url
« Reply #1 on: August 09, 2021, 06:59:35 am »
Hi, yes have a look at the example in the fcl-net package:
https://gitlab.com/freepascal.org/fpc/source/-/blob/main/packages/fcl-net/examples/testdns.pp#L66

The functions you're looking for are ResolveName and HostAddrToStr.

Ericktux

  • Sr. Member
  • ****
  • Posts: 345
Re: Get IP from website url
« Reply #2 on: August 09, 2021, 07:14:18 am »
hello friend, I just tried it and I get an error "netdb" is missing, I solve it and then another error "BaseUnix" is missing  :(

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: Get IP from website url
« Reply #3 on: August 09, 2021, 07:57:42 am »
You need to also add BaseUnix to your Uses clause.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Get IP from website url
« Reply #4 on: August 09, 2021, 09:24:13 am »
Doing it via the resolve unit might be cross unix/windows portable.

Ericktux

  • Sr. Member
  • ****
  • Posts: 345
Re: Get IP from website url
« Reply #5 on: August 14, 2021, 08:16:10 am »
hello friends, this code is working for me at the moment  :)

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, resolve;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Edit1: TEdit;
  17.     Memo1: TMemo;
  18.     procedure Button1Click(Sender: TObject);
  19.   private
  20.  
  21.   public
  22.  
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. function GetHostByName(HostName:String):String;
  35. var
  36.   host:THostResolver;
  37. begin
  38.   host := THostResolver.Create(nil);
  39.   if host.NameLookup(HostName) then
  40.      result := host.AddressAsString
  41.    else
  42.      result := '';
  43.   host.Free;
  44. end;
  45.  
  46. procedure TForm1.Button1Click(Sender: TObject);
  47. begin
  48.      //ShowMessage(GetHostByName('www.google.com'));
  49.      Memo1.Lines.Add(GetHostByName(Edit1.Text));
  50. end;
  51.  
  52. end.

 

TinyPortal © 2005-2018