Recent

Author Topic: THTTPsend  (Read 1077 times)

DanishMale

  • Jr. Member
  • **
  • Posts: 94
THTTPsend
« on: August 01, 2025, 01:33:09 pm »
Hi

I am a total newbie to Synapse and HTTPsend and don't seem to be able to find the correct settings

I have written this code and it returns 500 - Internal server error

Code: Pascal  [Select][+][-]
  1. function GetHTTP(webaddress: String; headerdata: TStringList = nil): String;
  2. const
  3.   WEB_USERAGENT : Array of String = (
  4.   'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36',
  5.   'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0',
  6.   'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0'
  7.   );
  8. begin
  9.   WEBdata := THTTPsend.Create;
  10.   try
  11.     WEBdata.Clear;
  12.     WEBdata.Protocol:='1.1';
  13.     WEBdata.UserAgent := WEB_USERAGENT[RandomBetween(0, Length(WEB_USERAGENT)-1)];
  14.     WEBdata.KeepAlive := True;
  15.     WEBdata.MimeType := headerdata[0];
  16.     headerdata.Delete(0);
  17.     WEBdata.Headers.AddStrings(headerdata);
  18.     if not WEBdata.HTTPMethod('GET', webaddress) then
  19.     begin
  20.       raise exception.create(IntToStr(WEBdata.ResultCode) + ' - ' + GetHTTPerrorText(WEBdata.ResultCode));
  21.     end;
  22.     Result := WEBdata.Document.ReadAnsiString;
  23.   except
  24.     on E: Exception do
  25.     begin
  26.       Result := 'ERROR - '+E.Message+LineEnding+LineEnding+IntToStr(WEBdata.ResultCode) +
  27.                 ' - ' + GetHTTPerrorText(WEBdata.ResultCode) +
  28.                 LineEnding+LineEnding+WEBdata.Headers.Text;
  29.     end;
  30.   end;
  31.   headerdata := nil;
  32.   WEBdata.Free;
  33. end;
  34.  

and is called with this code:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. begin
  3.   Memo1.Clear;
  4.   HeaderList := TStringList.Create;
  5.   HeaderList.Clear;
  6.   HeaderList.Add('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');
  7.   HeaderList.Add('Accept: */*; q=0.01');
  8.   HeaderList.Add('Accept-Language: da,en-US;q=0.7,en;q=0.3');
  9.   HeaderList.Add('Accept-Encoding: gzip, deflate, br, zstd');
  10.   Memo1.Lines.Add(GetHTTP('https://www.tnt.fr', HeaderList));
  11. end;
  12.  

I have added laz_synapse to the project and upgraded to ssl_openssl3 in synapse also downloaded the latest DLL's in x64.

I wonder what I can't see or overlooking..........

Any help is really appreciated  :D
« Last Edit: August 01, 2025, 02:08:54 pm by DanishMale »
Lazarus 4.4 x64 | Windows 10 x64 | Windows Server 2019 x64 | OpenViX 6.7.015 (Linux) | MySQL Community Server 8.0 x64 | MariaDB 12.0.2 x64 | SQLite 3.40.0 x64

rvk

  • Hero Member
  • *****
  • Posts: 6953
Re: THTTPsend
« Reply #1 on: August 01, 2025, 01:38:52 pm »
First line in your header string list is
Content-Type: text/html

You take that and put it in thhtpsend.mimetype.
The ENTIRE line.
That's incorrect. You need to only take the value after : and out it in mimetype.

DanishMale

  • Jr. Member
  • **
  • Posts: 94
Re: THTTPsend
« Reply #2 on: August 01, 2025, 01:45:32 pm »
First line in your header string list is
Content-Type: text/html

You take that and put it in thhtpsend.mimetype.
The ENTIRE line.
That's incorrect. You need to only take the value after : and out it in mimetype.

Corrected , however, still same error ....
Lazarus 4.4 x64 | Windows 10 x64 | Windows Server 2019 x64 | OpenViX 6.7.015 (Linux) | MySQL Community Server 8.0 x64 | MariaDB 12.0.2 x64 | SQLite 3.40.0 x64

rvk

  • Hero Member
  • *****
  • Posts: 6953
Re: THTTPsend
« Reply #3 on: August 01, 2025, 02:49:19 pm »
First I'm not entirely sure why you want to set a MimeType (Content-Type) for a GET.

Second... you do get an error 500 when openssl isn't initialized properly.
Did you add ssl_openssl or ssl_openssl3 to your uses clause?
And put the correct openssl dll's in the exe directory.

Third... I'm not sure you can use THTTPSend.Document.ReadAnsiString to get the data.
It is a TMemoryStream so TStream.ReadAnsiString should work but for me it crapped out with a read stream exception.

You CAN'T use TStream.ReadAnsiString because that expects a length at the beginning of the stream (see the ReadBuffer (TheSize ) at the beginning).

You can use this snippet to put the content of the memorystream to a string;
Code: Pascal  [Select][+][-]
  1. function MemoryStreamToString(M: TMemoryStream): ansistring;
  2. begin
  3.   SetString(Result, PAnsiChar(M.Memory), M.Size);
  4. end;
« Last Edit: August 01, 2025, 02:53:43 pm by rvk »

Thaddy

  • Hero Member
  • *****
  • Posts: 18792
  • Glad to be alive.
Re: THTTPsend
« Reply #4 on: August 01, 2025, 03:39:30 pm »
I would not use Synapse for that anymore, but TFPHttpClient: better maintained, modern and standard.
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2.  uses
  3.   Sysutils, classes, fphttpclient, opensslsockets;
  4.  
  5. const
  6.   WEB_USERAGENT : Array of String = (
  7.   'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36',
  8.   'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0',
  9.   'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0'
  10.   );
  11.  
  12. var
  13.   Client: TFPHttpClient;
  14.   Page:TStrings;
  15. begin
  16.   Client := TFPHttpClient.Create(nil);
  17.   Page := TStringlist.Create;
  18.   try
  19.     Randomize;
  20.     Client.AddHeader('User-Agent', WEB_USERAGENT[Random(High(WEB_USERAGENT)+1)]);
  21.     Client.AddHeader('Content-Type','text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');
  22.     Client.AddHeader('Accept-Encoding', 'gzip, deflate, br, zstd');
  23.     Client.AddHeader('Accept-Language',' da,en-US;q=0.7,en;q=0.3');
  24.     Client.AddHeader('Accept', '*/*; q=0.01');
  25.     Client.AllowRedirect := true;
  26.     try
  27.       Client.Get('https://www.tnt.fr',Page);
  28.       Writeln(Page.Text);
  29.      // or simply writeln(Client.Get('https://www.tnt.fr',Page)) and save you a stringlist
  30.     except
  31.       on E:Exception do
  32.         writeln(e.message);
  33.     end;
  34.   finally
  35.     Page.Free;
  36.     Client.Free;
  37.   end;
  38. end.
« Last Edit: August 01, 2025, 03:52:53 pm by Thaddy »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

 

TinyPortal © 2005-2018