1
votes

I'm new in Delphi and also sorry for my bad English.
I'm using Embarcadero Rad Studio Delphi 10.3.

I need to create Site with delphi, so I create new project
"Delphi\web\webserver application\windows\stand-alone GUI application\vcl application"

in WebModuleUnit (visual view), window "structure" I chose branch of my page and in
windows "Object Inspector" go to page "events" --> I create event in "OnAction" field
Then I switch to code view and in "WMTestPageAction" procedure wrote this code

procedure TWebModule1.WMTestPageAction(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
  var
   tstStr:tstrings;
  const 
   cookieUserName='UserNameXYZ';
begin

  Response.Content :=
    '<!DOCTYPE html>'+
    '<html>'+
    '<head>'+
      '<title>Test page</title>'+
    '</head>'+
    '<body>';


tstStr:=TStringList.Create;

//Request.Create;
Request.ExtractCookieFields(tstsTr);
Response.Content :=Response.Content+'<div>test output<br>'+
'get cookie name User = '+cookieUserName+' ; Request.Cookie  = '+Request.Cookie +'<br>'+
'get cookie name User = '+cookieUserName+' ; inttostr(Request.Cookie.Length)  = '+inttostr(Request.Cookie.Length) +'<br>'+
'get cookie name User = '+cookieUserName+' ; Request.ContentFields.Text  = '+Request.ContentFields.Text +'<br>'+
'get cookie name User = '+cookieUserName+' ; Request.ContentFields.Values[''UserNameXYZ'']  = '+Request.ContentFields.Values['UserNameXYZ'] +'<br>'+
'get cookie name Token= '+cookieUserToken+' ; Request.ContentFields.Values[cookieUserToken] = '+Request.ContentFields.Values[cookieUserToken]+'<br>'+
' Request.Content = '+ Request.Content +'<br>'+
' tstsTr.Text = '+ tstsTr.Text +'<br>'+

'<br>Response<br>'+
' Response.HTTPRequest.Cookie '+ Response.HTTPRequest.Cookie +'<br>'+
' Response.Cookies.Count = '+ IntToStr(Response.Cookies.Count) +'<br>'+
' Response.GetCustomHeader(''name2'') = '+ Response.GetCustomHeader('name2') +'<br>'+

' Response.GetCustomHeader(''name2'') = '+ '' +'<br>'+

'</div>';

Response.Content :=Response.Content+'</body></html>';
end;

in "response" I can form my page and I use this to get test info,
I can set Cookie with Response.SetCookieField(scValues,scDomen,scPath,scExpiries,scSecure);
but I can't get cookie in delphi on loading page, all what I try I wrote in code.

in browser console on command document.cookie
I recive"name2=value2; UserNameXYZ=jack; token=sd78sd78s7d8s"

I want to check user session on each load of all the pages in my site.
Respone and Request in code belongs to module "Web.HTTPApp".

Please advice.

Edits

// 12:30 I comment line with:
Request.Create; //did'nt help
  • tstsTr variable created only for reason to get cookie, I think that I use something wrong or I miss something.
1
Why are you calling Request.Create? That doesn't belong, get rid of it. Also, you are leaking tstStr.Remy Lebeau
Request.Create probably simply destroys the Request contents(along with the cookies in it). You should not create the Request - it is already created and being passed to your procedure. I think just removing the line Request.Create will make it work.Matthias B

1 Answers

0
votes

finaly find info from embarcadero in 1999

procedure TWebModule1.WebModule1WebActionItem3Action(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
  i: integer;
begin
  Response.Content := '';
  for i := 0 to (Request.CookieFields.Count - 1) do
  begin
    Response.Content := Response.Content + '

' +
    Request.CookieFields[i] + '
';
  end;
  Response.Content := Response.Content + '';
end;

For Extract I Use

function getCookieFieldByName(Sender:TStrings;vCookieName:string):string;
var i:Integer;
begin
  result:='';
  for i := 0 to (Sender.Count - 1) do
  begin
   if Pos( vCookieName+'=',Sender[i] ) >0 then
   begin
    result:= Sender[i];
    result:= Copy(result, Pos('=',result)+1 );
    Break;
   end;
  end;
end;

and call looks like this

result := getCookieFieldByName(Request.CookieFields,cookieUserName);