Difference between revisions of "fphttpclient"

From Free Pascal wiki
Jump to navigationJump to search
m
(Added getting external ip address sample)
Line 23: Line 23:
 
<syntaxhighlight>
 
<syntaxhighlight>
 
s := TFPCustomHTTPClient.SimpleGet('http://a_site/a_page');
 
s := TFPCustomHTTPClient.SimpleGet('http://a_site/a_page');
 +
</syntaxhighlight>
 +
 +
=== Get external IP address ===
 +
If your computer is connected to the internet via a LAN (cabled or wireless), the IP address of your network card most probably is not your external IP address.
 +
 +
You can retrieve your external IP address from e.g. your router or an external site. The code below tries to get it from an external site (thanks to JoStudio on the forum for the inspiration: [http://lazarus.freepascal.org/index.php/topic,17506.msg110917.html#msg110917]):
 +
<syntaxhighlight>
 +
{$mode objfpc}{$H+}
 +
 +
uses
 +
  Classes, SysUtils, fphttpclient, RegexPr;
 +
 +
function GetExternalIPAddress: string;
 +
var
 +
  HTTPClient: TFPHTTPClient;
 +
  IPRegex: TRegExpr;
 +
  RawData: string;
 +
begin
 +
  try
 +
    HTTPClient:=TFPHTTPClient.Create(nil);
 +
    IPRegex:=TRegExpr.Create;
 +
    try
 +
      //returns something like:
 +
      {
 +
<html><head><title>Current IP Check</title></head><body>Current IP Address: 44.151.191.44</body></html>       
 +
      }
 +
      RawData:=HTTPClient.Get('http://checkip.dyndns.org');
 +
      // adjust for expected output; we just capture the first IP address now:
 +
      IPRegex.Expression:=RegExprString('\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b');
 +
      //or
 +
      //\b(?:\d{1,3}\.){3}\d{1,3}\b
 +
      if IPRegex.Exec(RawData) then
 +
      begin
 +
        result:=IPRegex.Match[0];
 +
      end
 +
      else
 +
      begin
 +
        result:='Got invalid results getting external IP address. Details:'+LineEnding+
 +
          RawData;
 +
      end;
 +
    except
 +
      on E: Exception do
 +
      begin
 +
        result:='Error retrieving external IP address: '+E.Message;
 +
      end;
 +
    end;
 +
  finally
 +
    HTTPClient.Free;
 +
    IPRegex.Free;
 +
  end;
 +
end;
 +
 +
begin
 +
  writeln('External IP address:');
 +
  writeln(GetExternalIPAddress);
 +
end.
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:FPC]]
 
[[Category:FPC]]
 
[[Category:Networking]]
 
[[Category:Networking]]

Revision as of 13:21, 27 January 2013

fphttpclient is supplied with FPC as part of the fcl-web package, and can be used by itself as well.

Examples

Get body of an HTTP page

uses fphttpclient;

Var
  S : String;

begin
  With TFPHttpClient.Create(Nil) do
    try
      S:=Get(ParamStr(1));
  finally
    Free;
  end;
  Writeln('Got : ',S);
end.

If you want to write even less lines of code, in FPC 2.7.1 you can use the class method:

s := TFPCustomHTTPClient.SimpleGet('http://a_site/a_page');

Get external IP address

If your computer is connected to the internet via a LAN (cabled or wireless), the IP address of your network card most probably is not your external IP address.

You can retrieve your external IP address from e.g. your router or an external site. The code below tries to get it from an external site (thanks to JoStudio on the forum for the inspiration: [1]):

{$mode objfpc}{$H+}

uses
  Classes, SysUtils, fphttpclient, RegexPr;

function GetExternalIPAddress: string;
var
  HTTPClient: TFPHTTPClient;
  IPRegex: TRegExpr;
  RawData: string;
begin
  try
    HTTPClient:=TFPHTTPClient.Create(nil);
    IPRegex:=TRegExpr.Create;
    try
      //returns something like:
      {
<html><head><title>Current IP Check</title></head><body>Current IP Address: 44.151.191.44</body></html>        
      }
      RawData:=HTTPClient.Get('http://checkip.dyndns.org');
      // adjust for expected output; we just capture the first IP address now:
      IPRegex.Expression:=RegExprString('\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b');
      //or
      //\b(?:\d{1,3}\.){3}\d{1,3}\b
      if IPRegex.Exec(RawData) then
      begin
        result:=IPRegex.Match[0];
      end
      else
      begin
        result:='Got invalid results getting external IP address. Details:'+LineEnding+
          RawData;
      end;
    except
      on E: Exception do
      begin
        result:='Error retrieving external IP address: '+E.Message;
      end;
    end;
  finally
    HTTPClient.Free;
    IPRegex.Free;
  end;
end;

begin
  writeln('External IP address:');
  writeln(GetExternalIPAddress);
end.