fphttpwebclient.pp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. { **********************************************************************
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2015 by the Free Pascal development team
  4. FPHTTPClient implementation of TFPWebclient.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit fphttpwebclient;
  12. {$mode objfpc}{$H+}
  13. interface
  14. uses
  15. Classes, SysUtils, fpwebclient, fphttpclient;
  16. Type
  17. { TFPHTTPRequest }
  18. TFPHTTPRequest = Class(TWebClientRequest)
  19. Private
  20. FHTTP : TFPHTTPClient;
  21. Public
  22. function GetHeaders: TStrings;override;
  23. Constructor Create(AHTTP : TFPHTTPClient);
  24. Destructor Destroy; override;
  25. end;
  26. { TFPHTTPRequest }
  27. TFPHTTPResponse = Class(TWebClientResponse)
  28. Private
  29. FHTTP : TFPHTTPClient;
  30. Protected
  31. function GetHeaders: TStrings;override;
  32. Function GetStatusCode : Integer; override;
  33. Function GetStatusText : String; override;
  34. Public
  35. Constructor Create(AHTTP : TFPHTTPRequest);
  36. end;
  37. { TFPHTTPWebClient }
  38. TFPHTTPWebClient = Class(TAbstractWebClient)
  39. Protected
  40. Function DoCreateRequest: TWebClientRequest; override;
  41. Function DoHTTPMethod(Const AMethod,AURL : String; ARequest : TWebClientRequest) : TWebClientResponse; override;
  42. end;
  43. implementation
  44. uses dateutils;
  45. { TFPHTTPRequest }
  46. function TFPHTTPRequest.GetHeaders: TStrings;
  47. begin
  48. Result:=FHTTP.RequestHeaders;
  49. end;
  50. constructor TFPHTTPRequest.Create(AHTTP: TFPHTTPClient);
  51. begin
  52. FHTTP:=AHTTP;
  53. end;
  54. destructor TFPHTTPRequest.Destroy;
  55. begin
  56. FreeAndNil(FHTTP);
  57. inherited Destroy;
  58. end;
  59. { TFPHTTPResponse }
  60. function TFPHTTPResponse.GetHeaders: TStrings;
  61. begin
  62. if Assigned(FHTTP) then
  63. Result:=FHTTP.ResponseHeaders
  64. else
  65. Result:=Inherited GetHeaders;
  66. end;
  67. Function TFPHTTPResponse.GetStatusCode: Integer;
  68. begin
  69. if Assigned(FHTTP) then
  70. Result:=FHTTP.ResponseStatusCode
  71. else
  72. Result:=0;
  73. end;
  74. Function TFPHTTPResponse.GetStatusText: String;
  75. begin
  76. if Assigned(FHTTP) then
  77. Result:=FHTTP.ResponseStatusText
  78. else
  79. Result:='';
  80. end;
  81. Constructor TFPHTTPResponse.Create(AHTTP: TFPHTTPRequest);
  82. begin
  83. Inherited Create(AHTTP);
  84. FHTTP:=AHTTP.FHTTP;
  85. end;
  86. { TFPHTTPWebClient }
  87. Function TFPHTTPWebClient.DoCreateRequest: TWebClientRequest;
  88. Var
  89. C : TFPHTTPClient;
  90. begin
  91. C:=TFPHTTPClient.Create(Self);
  92. C.RequestHeaders.NameValueSeparator:=':';
  93. C.ResponseHeaders.NameValueSeparator:=':';
  94. // C.HTTPversion:='1.0';
  95. Result:=TFPHTTPRequest.Create(C);
  96. end;
  97. Function TFPHTTPWebClient.DoHTTPMethod(Const AMethod, AURL: String;
  98. ARequest: TWebClientRequest): TWebClientResponse;
  99. Var
  100. U,S : String;
  101. h : TFPHTTPClient;
  102. begin
  103. U:=AURL;
  104. H:=TFPHTTPRequest(ARequest).FHTTP;
  105. S:=ARequest.ParamsAsQuery;
  106. if (S<>'') then
  107. begin
  108. if Pos('?',U)=0 then
  109. U:=U+'?';
  110. U:=U+S;
  111. end;
  112. Result:=TFPHTTPResponse.Create(ARequest as TFPHTTPRequest);
  113. try
  114. if Assigned(ARequest.Content) and (ARequest.Headers.IndexOfName('Content-length')<0) then
  115. H.AddHeader('Content-length',IntToStr(ARequest.Content.size));
  116. if ARequest.Content.Size>0 then
  117. begin
  118. H.RequestBody:=ARequest.Content;
  119. H.RequestBody.Position:=0;
  120. end;
  121. H.HTTPMethod(AMethod,U,Result.Content,[]); // Will raise an exception
  122. except
  123. FreeAndNil(Result);
  124. Raise;
  125. end;
  126. end;
  127. end.