这个问题我在exex上花了50分,好心痛啊
response.content := '<script>document.cookie="username=' + username + ';password=' + encodedpassword + 'expire etc........;"'</script>';
Technical Information Database
TI4523D.txt - Working with cookies
Category :Internet/WEB
Platform :All-32Bit
Product :All32Bit,
Description:
The purpose of this document is not to explain what a cookie is or what it is used for. Instead this document
assumes that you are familiar with the use of cookies and want to know how to work with them when
developing web server applications with Delphi 3.
The TWebRequest and TWebResponse objects provided in Delphi 3 both have properties which allow for
the easy use of cookies. TWebRequest has the Cookie and CookieFields properties to allow a web server
application to read the cookie header sent as part of a HTTP request. The TWebResponse object has the
Cookies property which allows a web server application to place a cookie on the client machine through the
cookie header of the HTTP response. This is usually set through the use of the SetCookieField method.
When a server responds to a HTTP request it sends a document with a header and a content section. Delphi
provides the ability to add a cookie header through the TWebResponse.Cookies property. This is best set
through the use of the SetCookieField method. The following TWebActionItem demonstrates the use of the
SetCookieField method to return a cookie to the requesting browser. This example uses localhost as the
domain. You would use this for testing and replace this string with your domain name. The third paramater is
a slash. This means that this cookie will be sent along with all requests from the browser while at this domain.
For further explainations of these paramaters see the aforementioned RFC 2109.
procedure TWebModule1.WebModule1WebActionItem1Action(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
MyCookies: TStringList;
begin
MyCookies := TStringList.Create;
with MyCookies do begin
Add('Name=Frank Borland');
Add('Address=100 Borland Way');
Add('Product=Delphi');
end;
with Response do begin
SetCookieField(MyCookies, 'localhost', '/', (Now + 1), False);
Content := 'Cookie planted';
end;
end;
When a request is made of the HTTP server the client browser issuing the request will package up all of the
aplicable cookie name value pairs and include them in the HTTP requests cookie header. Delphi 3 will make
these available to a web server application in two ways. The first way is as a string through the Cookie
property of the TWebRequest paramater of a TWebActionItem. It is also available as a TStrings property
called CookieFields. CookieFields is the parsed contents of the Cookie header of an HTTP request message.
The following are TWebActionItems which extract the name value pairs and return them to the client in the
form of a bare bones HTML page. The first example returns the cookie as a single string, while the second
example returns each name value pair on a seperate line.
procedure TWebModule1.WebModule1WebActionItem2Action(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
Response.Content := '<HTML><BODY>' + Request.Cookie +
'</BODY></HTML>';
end;
procedure TWebModule1.WebModule1WebActionItem3Action(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
i: integer;
begin
Response.Content := '<HTML><BODY>';
for i := 0 to (Request.CookieFields.Count - 1) do
begin
Response.Content := Response.Content + '<p>' +
Request.CookieFields + '</p>';
end;
Response.Content := Response.Content + '</BODY></HTML>';
end;
//end TI
Reference:
None
4/1/99 11:45:44 AM
//////////////////////////////////////////////////////////
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.