<div dir="ltr"><div><div><div><div><div><div>Hello,<br><br></div>The current version of the TFPHTTPClient (the one shipped with fpc 3.0) is coupled to the fpc's sockets framework (fcl-net) so we can't utilize other tcp connection framework (e.g Synapse) to perform HTTP requests using TFPHTTPClient. <br></div><br></div>I have made an improvement to the TFPHTTPClient class (unit
fphttpclient, package fcl-web) which consists of decoupling the client
from the socket framework that is used to perform HTTP requests. <br><br></div><span id="result_box" class="" lang="en"><span>My</span> <span>solution is quite</span> <span class="">simple: i created a new class called TTCPConnectionAdapter (as the name suggests, i used the Adapter pattern) and changed the http client class to perform requests using a TCPConnectionAdapter instance . The connection adapter is passed as a parameter to the constructor of the TFPHTTPClient class (dependency injection). So, the TFPHTTPClient is not tied to the a particular socket framework anymore. If one wants to perform http requests using e.g Synapse, just implement the appropriate TTCPConnectionAdapter descendant and inject it into TFPHTTPClient.<br><br></span></span></div><span id="result_box" class="" lang="en"><span class="">The code below shows the TInetAdapter<br><br><br>======================================<br><br>{ </span></span><span id="result_box" class="" lang="en"><span class=""><span id="result_box" class="" lang="en"><span class="">TTCPConnectionAdapter</span></span> }<br><br>TTCPConnectionAdapter = class(TStream)<br> strict private<br> FHost: String;<br> FPort: Word;<br> procedure ValidateConnection;<br> strict protected<br> procedure DoConnect(const AHost: String; const APort: Word); virtual; abstract;<br> function DoRead(var Buffer; Count: Longint): Longint; virtual; abstract;<br> function DoWrite(const Buffer; Count: Longint): Longint; virtual; abstract;<br> function GetConnected: Boolean; virtual; abstract;<br> public<br> procedure Connect(const AHost: String; const APort: Word);<br> procedure Disconnect; virtual; abstract;<br> function Read(var Buffer; Count: Longint): Longint; override;<br> function Seek(Offset: Longint; Origin: Word): Longint; override;<br> function Write(const Buffer; Count: Longint): Longint; override;<br> property Connected: Boolean read GetConnected;<br> property Host: String read FHost;<br> property Port: Word read FPort;<br> end; <br><br>{ TInetConnectionAdapter }<br><br> TInetConnectionAdapter = class(TTCPConnectionAdapter)<br> strict private<br> FSocket: TInetSocket;<br> procedure FreeSocket;<br> strict protected<br> procedure DoConnect(const AHost: String; const APort: Word); override;<br> function DoRead(var Buffer; Count: Longint): Longint; override;<br> function DoWrite(const Buffer; Count: Longint): Longint; override;<br> function GetConnected: Boolean; override;<br> public<br> destructor Destroy; override;<br> procedure Disconnect; override;<br> end; <br><br> { TFPCustomHTTPClient }<br><br> TFPCustomHTTPClient = Class(TComponent)<br> private<br> ......<br> FTCPConnection: TTCPConnectionAdapter; <br> public<br> Constructor Create(const ATCPConnection: TTCPConnectionAdapter);<br></span></span></div><span id="result_box" class="" lang="en"><span class=""> end; <br></span></span><div><span id="result_box" class="" lang="en"><span class=""><br>==============================================================<br></span></span><div><div><div><span id="result_box" class="" lang="en"><span class=""><br></span></span></div><div><span id="result_box" class="" lang="en"><span class="">Best regards<br></span></span></div><div><span id="result_box" class="" lang="en"><span class=""><br></span></span></div></div></div></div></div>