[SOLVED] How to clear TFPHTTPClient cache? lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- senglit · 2022-03-03 15:55 hi all, I got a simple example code to read an xml file from a web page like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  http: TFPHTTPClient;  Response: TMemoryStream;  res: string;begin  http := TFPHTTPClient.Create(nil);  Response := TMemoryStream.Create;  http.AddHeader('Accept', 'text/html,application/xhtml+xml,application/xml');  http.AddHeader('Accept-Encoding', 'dflate');  http.AddHeader('Cache- Control', 'max-age=0');  http.AddHeader('Connection', 'keep-alive');  http.KeepConnection := True;  while updating do begin    http.Get('http://127.0.0.1/ajax/F_refresh.asp?Mode=RT', Response);    Response.Seek(0, soFromBeginning);    res := StreamToString(Response);          // <------  this is what I want    sleep(1000);  end;end; function TForm1.StreamToString(aStream: TStream): string;var  SS: TStringStream;begin  if aStream <> nil then begin    SS := TStringStream.Create('');    try      SS.CopyFrom(aStream, 0);      Result := SS.DataString;    finally      SS.Free;    end;  end else begin    Result := '';  end;end;           I'm sure the server will refresh the page every 0.5 sec. And there is a timestamp in the returned xml text. It can be confirmed by a webbrowser. But with this code, I got the same value in 'res' everytime. If I free the httpclient and create it before I send the GET request then I can get the correct value in 'res'. I think there must be a cache somewhere with TFPHTTPClient and I didn't find the way to clear it. I just want to save the cpu resources and don't want to recreate it every time. how should I do? ---------------------------------------------------------------------- lainz · 2022-03-03 15:59 Hi, I had the same problem and finally the best was to recreate the FPHTTPClient. ---------------------------------------------------------------------- senglit · 2022-03-03 17:14 haha~~~ After digging a little bit into the source code of TFPHTTPClient.Get, I found a way to solve this problem. Do not use TFPHTTPClient.Get This works:      TFPHTTPClient.HTTPMethod('GET',url,Response,[200]); ---------------------------------------------------------------------- Leledumbo · 2022-03-03 18:18 Quote from: senglit on March 03, 2022, 06:14:19 pmhaha~~~ After digging a little bit into the source code of TFPHTTPClient.Get, I found a way to solve this problem. Do not use TFPHTTPClient.Get This works:      TFPHTTPClient.HTTPMethod('GET',url,Response,[200]);   Funny because if you look at Get implementation, it actually just wraps the call to HTTPMethod. ResetResponse is called on the start of every call to HTTPMethod, which cleans up the response. I think what you actually need is to call: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Response.Seek(0, soFromBeginning);again after StreamToString. The ReadResponse method which is responsible to write the response to stream parameter does not do anything to the passed stream except calling Write, so it's your responsibility to ensure that its internal pointer is at the beginning. Otherwise, you just keep appending. You can prove this by printing out the stream's Size on each iteration, you'll see it getting bigger and bigger. ---------------------------------------------------------------------- Thaddy · 2022-03-03 18:44 And that is the correct answer... ---------------------------------------------------------------------- senglit · 2022-03-04 01:56 You are right. What actually worked is not recreate fphttpclient, but recreate response. I need to do Response.Clear every time before http.get Quote from: Leledumbo on March 03, 2022, 07:18:04 pmQuote from: senglit on March 03, 2022, 06:14:19 pmhaha~~~ After digging a little bit into the source code of TFPHTTPClient.Get, I found a way to solve this problem. Do not use TFPHTTPClient.Get This works:      TFPHTTPClient.HTTPMethod('GET',url,Response,[200]);   Funny because if you look at Get implementation, it actually just wraps the call to HTTPMethod. ResetResponse is called on the start of every call to HTTPMethod, which cleans up the response. I think what you actually need is to call: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Response.Seek(0, soFromBeginning);again after StreamToString. The ReadResponse method which is responsible to write the response to stream parameter does not do anything to the passed stream except calling Write, so it's your responsibility to ensure that its internal pointer is at the beginning. Otherwise, you just keep appending. You can prove this by printing out the stream's Size on each iteration, you'll see it getting bigger and bigger.