cookie problem?

procedure TWebModuleWord.WebModuleItemLoginAction(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
  UserName,UserPwd:string;
  Tempstr:tstrings;
begin
  if request.methodtype=mtpost then
  begin
    UserName:='';UserPwd:='';
    TempStr:=TStringList.Create;
    request.ExtractContentFields(TempStr);
    UserName:=GetValueOfPost(TempStr.Strings[0]);
    UserPwd:=GetValueOfPost(TempStr.Strings[1]);
    TempStr.Free;
    //Ìí¼Ócookie
    with Response.Cookies.Add do
    begin
      Name:='UserName';
      Value:=UserName;
    end;
    with Response.Cookies.Add do
    begin
      Name:='UserPwd';
      Value:=UserPwd;
    end;
    Response.SendRedirect('http://hubdog/hubdog.exe/hubdog');
    end;
  end;
end;

procedure TWebModuleWord.WebModuleWordItemMainAction(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
  UserName:string;
begin
  if request.CookieFields.Values['UserName']<> '' then
  begin
    PageDynamic.HTMLFile:=ExtractFilePath(Paramstr(0))+'Test.htm';
    Response.content:=PageDynamic.Content;
  end
  else
    Response.SendRedirect('http://hubdog/happyword/happyword.exe/Login');
end;

After having added two cookies and sendredirect to MainActionItem,
the MainActionItem can not fetch the cookies added before and return to loginitem.
why and how to solve the problem?

thank you very much

hubdog
LVL 2
hubdogAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

EpsylonCommented:
Response.Cookies.Add does not work.
Store the cookies in a StringList (e.g. MyCookies):

Response.SetCookieField(MyCookies, '', '/', Now + 1, false);


Eps.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hubdogAuthor Commented:
hi Epsylon
procedure TWebResponse.SetCookieField(Values: TStrings; const ADomain,
  APath: string; AExpires: TDateTime; ASecure: Boolean);
var
  I: Integer;
begin
  for I := 0 to Values.Count - 1 do
    with Cookies.Add do
    begin
      Name := Values.Names[I];
      Value := Values.Values[Values.Names[I]];
      Domain := ADomain;
      Path := APath;
      Expires := AExpires;
      Secure := ASecure;
    end;
end;
  I look the source of setcookiefield and didn't found any important difference from my code.
  And I follow your suggestion like this
    Cookies:=TStringList.Create;
    Cookies.Add('UserName=hubdog');
    Cookies.Add('UserPwd=Sysoft');
    Response.SetCookieField(Cookies, '', '/', Now+1000, false);
and not effect.

Could you explain it more clearly

thanks in advance

hubdog

0
EpsylonCommented:
It should work. Doesn't it write a cookie to your windows\cookies directory?
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

hubdogAuthor Commented:
hi Epsylon
  I think the main problem is if I use response.content:='login correctly' then no problem.And if I use response.sendredirect('http://xxx') after having added cookie,then I can't add cookie successfully. Could you test it and tell me why?

thanks in advance

hubdog
0
EpsylonCommented:
Yep, it won't work with SendRedirect. Here's something about it that I found a while ago:

-----------------------------------------------------

Here is my solution for checking the cookies.
In the code I wrote
---
procedure TWebModule1.WebModule1WebActionItem1Action(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var cookielist : TstringList;
begin
  cookielist := TStringList.Create;
  try
    cookielist.Add('Name1=alice');
    Response.SetCookieField(cookielist,'','',-1, false);
  finally
    cookielist.free;
  end;
  Response.Content := '<html><head><meta http-eqiv="Refresh"
Content="0;URL=http://mywebsite/mydll.dll/checkcookie"></head></html>';
  Handled := true;
end;

procedure TWebModule1.WebModule1WebActionItem3Action(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var s : string;
begin
  s := '';
  try
    s := Request.CookieFields.Values['Name1'];
  except
    {silent}
  end;
  if Length(s) <> 0 then
    Response.SendRedirect('http://mywebsite/myhtml.htm')
  else
    Response.Content := 'Sorry, You do not accept the cookies.';
end;
---------
Since the SetCookieFields doesn't work with SendRedirect, I display the
temperary pape. Then the temperary page will refresh in 0 second and run the
mydll.dll/checkcookies,redirect the page.
It works for IE4,5 and NS4 from PWS.  It seems ASP doing the same way.
*************************
Still question:
from delphi4 help file, it says

procedure SendRedirect(const URI: string); virtual; abstract;
Description
Call SendRedirect to redirect the web client request to another URI that is the
proper target of the request. SendRedirect does the same thing as setting the
StatusCode to 301, the Location to URI, and calling SendResponse.

But I wrote the code as
  Response.StatusCode:=301;
  Response.Location := 'http://mywebsite/myhtml.htm';
  Response.SendResponse;
---
It doens't work for me at all. I guest the help file is wrong.

------------------------------------

"Hadi Hariri (Team Winshoes)" wrote:

> Well, you already sent me an e-mail, so I'll be brief:
>
> You have to use SendRedirect to redirect. Problem: Not compatible with
> SetCookieField. I have been trying to do a workaround using SetCustomHeader
> and Location and StatusCode props of response. More news I'll let you know.
>
> Alice Lius escribió en mensaje <379C9DC7.CED9F387@yahoo.com>...
> >I have to use SendResponse after SetCookieFields.   So how I redirect the
> >repoonse to another entry point in my dll.  Please help.
> >
> >Hadi Hariri wrote:
> >
> >> Well, one way I can think of is that once you set the cookie, redirect
> the
> >> response to another entry point in your dll to read the cookies. If the
> >> cookies are present then redirect to the original page you were intending
> to
> >> redirect after placing cookies. If not then display an error.
> >>
> >> Alice Liu escribió en mensaje <3798D6F5.7EEECDCA@yahoo.com>...
> >> >Somebody can help this.
> >> >If I create cookies from the login screen, how the .dll know if the user
> >> >accept this cookies or not.
> >> >---
> >> >alice
> >> >
0
hubdogAuthor Commented:
thank you very much!!!

hubdog
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.