https://wiki.freepascal.org/api.php?action=feedcontributions&user=Agyuri&feedformat=atomFree Pascal wiki - User contributions [en]2024-03-28T09:03:07ZUser contributionsMediaWiki 1.35.6https://wiki.freepascal.org/index.php?title=fphttpclient&diff=104554fphttpclient2016-10-20T16:43:12Z<p>Agyuri: /* Upload a file using POST */</p>
<hr />
<div>==Overview==<br />
fphttpclient is supplied with FPC as part of the [[fcl-web]] package, and can be used by itself as well.<br />
<br />
== HTTPS (TLS/SSL) ==<br />
Since April 2014, the trunk/development fphttpclient supports SSL/TLS connections using the OpenSSL library (will ship with version 2.8.0 and above). This requires the OpenSSL .so/.dll/.dylib library/libraries to be installed (e.g. present in your application or system directory on Windows).<br />
* If you do not use client side certificates, just specifying the proper port (e.g. 443 for https) is enough to enable TLS/SSL as long as you have OpenSSL libraries installed (or e.g. in the application directory)<br />
* If you want to use e.g. a client side certificate, do something like this:<br />
<syntaxhighlight><br />
uses ...ssockets, sslsockets..<br />
// Callback for setting up SSL client certificate<br />
procedure TSSLHelper.SSLClientCertSetup(Sender: TObject; const UseSSL: Boolean;<br />
out AHandler: TSocketHandler);<br />
begin<br />
AHandler:=nil;<br />
if UseSSL and (FClientCertificate<>'') then<br />
begin<br />
// Only set up client certificate if needed.<br />
// If not, let normal fphttpclient flow create<br />
// required socket handler<br />
AHandler:=TSSLSocketHandler.Create;<br />
// Example: use your own client certificate when communicating with the server:<br />
(AHandler as TSSLSocketHandler).Certificate.FileName:=FClientCertificate;<br />
end;<br />
end;<br />
<br />
//... and in your TFPHTTPClient creation:<br />
myclient:=TFPHTTPClient.Create(nil);<br />
if FClientCertificate<>'' then<br />
myclient.OnGetSocketHandler:=@SSLClientCertSetup;<br />
</syntaxhighlight><br />
<br />
== Examples ==<br />
Examples are included in your FPC directory: packages/fcl-web/examples/<br />
<br />
Apart from those, please see below:<br />
<br />
=== Get body of a web page via HTTP protocol ===<br />
<syntaxhighlight><br />
uses fphttpclient;<br />
<br />
Var<br />
S : String;<br />
<br />
begin<br />
With TFPHttpClient.Create(Nil) do<br />
try<br />
S:=Get(ParamStr(1));<br />
finally<br />
Free;<br />
end;<br />
Writeln('Got : ',S);<br />
end. <br />
</syntaxhighlight><br />
<br />
If you want to write even less lines of code, in FPC 2.7.1 you can use the class method:<br />
<syntaxhighlight><br />
s := TFPCustomHTTPClient.SimpleGet('http://a_site/a_page');<br />
</syntaxhighlight><br />
<br />
=== Upload a file using POST ===<br />
Use TFPHTTPClient.FileFormPost()<br />
<syntaxhighlight><br />
uses fphttpclient;<br />
<br />
Var<br />
Respo: TStringStream;<br />
S : String;<br />
<br />
begin<br />
With TFPHttpClient.Create(Nil) do<br />
try<br />
Respo:=TStringStream.Create('');<br />
FileFormPost('http://example.com/upload.php','PostFilenameParam',edtSourceFile.Text,Respo);<br />
S:=Respo.DataString;<br />
Respo.Destroy;<br />
finally<br />
Free;<br />
end;<br />
end.<br />
</syntaxhighlight><br />
<br />
=== Get external IP address ===<br />
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.<br />
<br />
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]):<br />
<syntaxhighlight><br />
{$mode objfpc}{$H+}<br />
<br />
uses<br />
Classes, SysUtils, fphttpclient, RegexPr;<br />
<br />
function GetExternalIPAddress: string;<br />
var<br />
HTTPClient: TFPHTTPClient;<br />
IPRegex: TRegExpr;<br />
RawData: string;<br />
begin<br />
try<br />
HTTPClient:=TFPHTTPClient.Create(nil);<br />
IPRegex:=TRegExpr.Create;<br />
try<br />
//returns something like:<br />
{<br />
<html><head><title>Current IP Check</title></head><body>Current IP Address: 44.151.191.44</body></html> <br />
}<br />
RawData:=HTTPClient.Get('http://checkip.dyndns.org');<br />
// adjust for expected output; we just capture the first IP address now:<br />
IPRegex.Expression:=RegExprString('\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b');<br />
//or<br />
//\b(?:\d{1,3}\.){3}\d{1,3}\b<br />
if IPRegex.Exec(RawData) then<br />
begin<br />
result:=IPRegex.Match[0];<br />
end<br />
else<br />
begin<br />
result:='Got invalid results getting external IP address. Details:'+LineEnding+<br />
RawData;<br />
end;<br />
except<br />
on E: Exception do<br />
begin<br />
result:='Error retrieving external IP address: '+E.Message;<br />
end;<br />
end;<br />
finally<br />
HTTPClient.Free;<br />
IPRegex.Free;<br />
end;<br />
end;<br />
<br />
begin<br />
writeln('External IP address:');<br />
writeln(GetExternalIPAddress);<br />
end.<br />
</syntaxhighlight><br />
<br />
=== Translate text using Google Translate ===<br />
See [[Using Google Translate]]<br />
<br />
[[Category:Units]]<br />
[[Category:FPC]]<br />
[[Category:Networking]]</div>Agyuri