关于cookie的问题,高手请进(300分)

H

hubdog

Unregistered / Unconfirmed
GUEST, unregistred user!
我想在一个Actionitem中增加cookie,然后sendredirect到另一个actionitem
在那里验证cookie,有cookie这显示一个页面,否则返回.
但我发现增加完cookie后,如果马上sendredirect则另一个actionitem取不到cookie值(说明cookie增加不成功),若不马上sendredirect而是
response.content:='某一页面内容',然后用户点击连结到另一ActionItem
则可以成功读出cookie.Why ? Solution?

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.content:='right login';
//Response.SendRedirect('http://hubdog/hubdog.exe/hubdog');
end;
end;
end;
 
Cookie是一个Response的头(header)部分,必须在正文之前设置。如果用SendRedirect,只有新页面才会传送,SendRedirect之前设置的Response
会取消,导致Cookie不能加到新页面的头部分,所以设置Cookie不成功!
解决办法是:永远不要在设置Cookie后再用SendRedirect。你所说的问题可以这样
解决:在同一个ActionItem中设置和读取Cookie.不过这样做好象没有任何意义,
可能你只想做一个实验?

 
to fcd:
那为什么我添加完cookie,在同一个ActionItem中也取不到cookie

with Response.Cookies.Add do
begin
Name:='UserName';
Value:=UserName;
end;
with Response.Cookies.Add do
begin
Name:='UserPwd';
Value:=UserPwd;
end;

response.content:='right login'+request.cookiefields.values[UserName] ;
//只显示为right login用户名没有了
//Response.SendRedirect('http://hubdog/hubdog.exe/hubdog');
 
不合理啊!
1、为何在验证之后还要验证
2、为何验证不是独立模块
3、request.cookiefields.values['UserName']
其实可以这样response.content:='<pre>'+request.cookiefields.text+'</pre>';
看看到底是什么就知道了。
 
〈pre〉标签,又忘了
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
823
SUNSTONE的Delphi笔记
S
顶部