I want to make a HTTP post with TLS 1.2. I managed to make this work with Clever Internet Suite but failed with Synapse (downloaded 2025-02). I have to make a POST request in order to get a bearer (in JSON format) that I have to add in header in any following request (POST or GET).
This is used in both projects:
const
c_username = 'AUSERNAME';
c_password = 'APASSWORD';
c_client_id= 'ACLIENTID';
c_url = '';
Succeeded test with Clever Internet Suite
procedure TForm2.ButtonA2Click(Sender: TObject);
var AHTTP:TclHttp;
AReq:TclHttpRequest; AItem:TclHttpRequestItem;
s:string; aurl,FErrorDescr:string;
ResObj,HelpObj: TJsonObject; FTokenLimit:TDateTime;
begin
try
try
AHTTP:=TclHttp.Create(nil); Areq:= TclHttpRequest.create(nil);
with AHTTP do begin
HttpVersion:=hvHttp1_1;
TLSFlags:=[tfUseTLS12];
UseTLS:=ctAutomatic;
end;
with Areq do begin
AITem:=AReq.Items.Add(TclFormFieldRequestItem);
with TclFormFieldRequestItem(AITem) do begin
FieldName:='grant_type'; FieldValue:='password';
end;
AITem:=AReq.Items.Add(TclFormFieldRequestItem);
with TclFormFieldRequestItem(AITem) do begin
FieldName:='username'; FieldValue:=c_username;
end;
AITem:=AReq.Items.Add(TclFormFieldRequestItem);
with TclFormFieldRequestItem(AITem) do begin
FieldName:='password'; FieldValue:=c_password;
end;
AITem:=AReq.Items.Add(TclFormFieldRequestItem);
with TclFormFieldRequestItem(AITem) do begin
FieldName:='client_id'; FieldValue:=c_client_id;
end;
end;
aurl:=c_url;
s:=AHTTP.Post(aurl,Areq);
Memo1.Lines.add(s);
ResObj:=TJsonObject.Parse(s) as TJsonObject;
if AHTTP.StatusCode = 200 then begin
end else begin
end;
except
on E:Exception do begin
ShowMessage(E.Message);
end;
end;
finally
AHTTP.Free; AReq.Free;
end;
end;
Failed test with Ararat Synapse
{ *** Notes ***
the following DLLs (32bit, as my appl) are in application's folder:
libssl-1_1.dll : v.1.1.1.23 1.1.1w date 11.9.2023
libcrypto-1_1.dll : v.1.1.1.23 1.1.1w date 11.9.2023
libssl-3.dll : v.3.4.1.0 date 11.2.2025
libcrypto-3.dll : v.3.4.1.0 date 11.2.2025
What happens: MyHttpPostURL returns true (no error happened) but access is denied
> Access Denied
> You don't have permission to access "; on this server.<P>
>> Reference #18.777ecdd4.1739735221.243974bb
>> .777ecdd4.1739735221.243974bb
}
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs,System.strutils,Vcl.StdCtrls,Vcl.Graphics,
HttpSend,blcksock, synautil, synaip, synacode, synsock,
ssl_openssl11, ssl_openssl11_lib
//or (alternatively) ssl_openssl3_lib, ssl_openssl3
function MyHttpPostURL(const URL, URLData: string; const Data: TStream): Boolean;
{ MyHttpPostURL is exact copy of Synapse library's HttpPostUrl with the addition
of lines:
HTTP.protocol:='1.1';
HTTP.Sock.SSL.SSLType:=LT_TLSv1_2;
HTTP.UserAgent:='Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0';
So the WriteStrToStream procedure is Synapse original code
}
var
HTTP: THTTPSend;
begin
HTTP := THTTPSend.Create;
HTTP.protocol:='1.1';
HTTP.Sock.SSL.SSLType:=LT_TLSv1_2;
HTTP.UserAgent:='Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0';
try
WriteStrToStream(HTTP.Document, URLData);
HTTP.MimeType := 'application/x-www-form-urlencoded';
Result := HTTP.HTTPMethod('POST', URL);
if Result then
Data.CopyFrom(HTTP.Document, 0);
finally
HTTP.Free;
end;
end;
procedure TForm2.SB_Auth2Click(Sender: TObject);
var MS:TMemoryStream; URL,params:string; AList:TStrings;
begin
try
MS:=TMemoryStream.create; AList:=TStringList.Create;
url:= c_url;
params:='grant_type='+EncodeURLElement('password')+'&'
+'username='+EncodeURLElement(c_username)+'&'
+'password='+EncodeURLElement(c_password)+'&'
+'client_id='+EncodeURLElement(c_client_id);
try
if MyHttpPostURL(URL, Params, MS) then begin
Memo1.Lines.add('Boolean:true');
end else begin
Memo1.Lines.add('Boolean:false');
end;
MS.Position:=0;
AList.LoadFromStream(MS);
memo1.Lines.add(AList.text);
except
on E:Exception do begin
ShowMessage(E.Message);
end;
end;
finally
MS.Free; AList.Free;
end;
end;