* * *

Author Topic: Cannot reuse TFPHTTPClient.  (Read 697 times)

ExSystem

  • New member
  • *
  • Posts: 18
Cannot reuse TFPHTTPClient.
« on: February 25, 2017, 08:55:19 am »
Code: Pascal  [Select]
  1. Program project1;
  2.  
  3. Uses
  4.   fphttpclient,
  5.   Classes,
  6.   SysUtils;
  7.  
  8. Var
  9.   FHttpClient: TFPHTTPClient;
  10.   FServerUri: String;
  11.   Controller: String;
  12.   Action: String;
  13.   mResponse: String;
  14. Begin
  15.   Try
  16.     Try
  17.       FServerUri := 'http://www.baidu.com/';
  18.       Controller := 'aa';
  19.       Action := 'bb';
  20.  
  21.       FHttpClient := TFPHTTPClient.Create(Nil);
  22.       FHttpClient.RequestBody := TStringStream.Create('{"Revisions":[]}');
  23.       FHttpClient.AddHeader('Content-Type', 'text/json');
  24.       mResponse := FHttpClient.Post(Format('%s%s/%s', [FServerUri, Controller, Action]));
  25.       FHttpClient.RequestBody.Free();
  26.       FHttpClient.RequestBody := Nil;
  27.       WriteLn('#1: ', mResponse);
  28.  
  29.       Action := 'cc';
  30.       mResponse := FHttpClient.FormPost(Format('%s%s/%s', [FServerUri, Controller, Action]), 'Page=1');
  31.       WriteLn('#2: ', mResponse);
  32.     Except
  33.       On e: Exception Do
  34.       Begin
  35.         WriteLn('######### ', E.Message);
  36.         ReadLn();
  37.       End;
  38.     End;
  39.   Finally
  40.     WriteLn('FINISH!');
  41.     ReadLn();
  42.     FHttpClient.Free();
  43.     FHttpClient := Nil;
  44.   End;
  45. End.
  46.  

As the code mentioned above, the second request cannot be finished and the program hung for a long time and exited. Why? Thanks.

I'm using FPC3.1.1/CodeTyphon 6.0/Windows10 (64bit)

GetMem

  • Hero Member
  • *****
  • Posts: 2042
Re: Cannot reuse TFPHTTPClient.
« Reply #1 on: February 25, 2017, 10:22:06 am »
Just clear the request headers between the two request:

Code: Pascal  [Select]
  1.   //...
  2.  WriteLn('#1: ', mResponse);
  3.  FHttpClient.RequestHeaders.Clear; //<-- add this line
  4.  Action := 'cc';  
  5.  //...

ExSystem

  • New member
  • *
  • Posts: 18
Re: Cannot reuse TFPHTTPClient.
« Reply #2 on: February 25, 2017, 01:05:38 pm »
Just clear the request headers between the two request:

Code: Pascal  [Select]
  1.   //...
  2.  WriteLn('#1: ', mResponse);
  3.  FHttpClient.RequestHeaders.Clear; //<-- add this line
  4.  Action := 'cc';  
  5.  //...

Quite confused. Header 'Content-Type' should be replaced by FormPost method, why Clear is required? Will it impact my cookies settings (eg. the 'login' thing) produced by the first request?
Code: Pascal  [Select]
  1. procedure TFPCustomHTTPClient.FormPost(const URL, FormData: string;
  2.   const Response: TStream);
  3.  
  4. begin
  5.   RequestBody:=TStringStream.Create(FormData);
  6.   try
  7.     AddHeader('Content-Type','application/x-www-form-urlencoded');
  8.     Post(URL,Response);
  9.   finally
  10.     RequestBody.Free;
  11.     RequestBody:=Nil;
  12.   end;
  13. end;
  14.  

BTW: How to stepping in the source code of TFPHTTPClient when debugging?...Really recompile it with debug info?
« Last Edit: February 25, 2017, 01:15:48 pm by ExSystem »

GetMem

  • Hero Member
  • *****
  • Posts: 2042
Re: Cannot reuse TFPHTTPClient.
« Reply #3 on: February 25, 2017, 03:48:46 pm »
Quote
Quite confused. Header 'Content-Type' should be replaced by FormPost method, why Clear is required? Will it impact my cookies settings (eg. the 'login' thing) produced by the first request?
Well apparently it does not get replaced by FormPost. I did not study how FormPost works internally, but you can find out by debugging the code.

Quote
TW: How to stepping in the source code of TFPHTTPClient when debugging?...Really recompile it with debug info?
Ideally you should recompile FPC with debug info, however in this particular case there is a shortcut. Just copy fphttpclient.pp from
$(FPCDir)\source\packages\fcl-web\src\base\  to your project folder and you're good to go.
« Last Edit: February 25, 2017, 04:25:00 pm by GetMem »

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus