不用什么Repson,Request的,因为我只想修改本地的cook。可是用InternetSetCookie总也没有效果,谁来教教我?
我是这样写的:
var
cookbuffer:array[0..20] of char
begin
cookbuffer[0]:='r';
InternetSetCookie(PChar('http://www.xxxx.com'),pchar('userName'),@cookBuffer);
end;
可是在IE文件夹里察看,总是没有改!(我在同样的地址用InternetGetCookie取出过COOK)
我是这样写的:
var
cookbuffer:array[0..20] of char
begin
cookbuffer[0]:='r';
InternetSetCookie(PChar('http://www.xxxx.com'),pchar('userName'),@cookBuffer);
end;
可是在IE文件夹里察看,总是没有改!(我在同样的地址用InternetGetCookie取出过COOK)
顺便问一下,怎么直接给cookbuffer变量赋值?
用10代替),这样就可以建立Cookie了. 以下为建立一个访问计数器的程序,可以参照:
const
LastIp:String='0:0:0:0'; //避免重复计数
Var
S:String;
Count:Integer;
begin
S:=Request.CookieFields.Values['count'];
if S< > '' then begin
try
Count:=StrToInt(S);
Except
Count:=0;
End;
end else Count:=0;
if LastIp< > Request.RemoteAddr then Inc(Count);
LastIp:=Request.RemoteAddr;
Response.SetCustomHeader('Set-Cookie','count='+IntToStr(Count)+';
expires=Sun, 21 Feb 2010 08:08:08 GMT');
Response.Content:='您是第'+IntToStr(Count)+'来到本页!';
End;
with response.cookies.add do
begin
name:='username';
value:='username';
end
Cookie中不应该有分号或逗号。
客户可以拒绝Cookie,所以任何一个Web应用程序不能认为客户的机器肯定有Cookie。
察看Cookie的情况可以在Windows\Cookies中找。
在Delphi中管理Cookie是一件非常容易的事。THTTPRequest和THTTPResponse都封装了用来处理Cookie的特性和方法,允许您控制怎样读写Cookie。
要设置Cookie的内容,您可以调用TWebResponse的SetCookieField的方法。这个方法需要传递一个TStrings对象作为Cookie的内容以及这个Cookie的限制条件。
SetCookieField()方法是这样声明的:
procedure SetCookieField(Values:TStrings;const ADomain,APath:string;AExpires:TDateTime;ASecure:Boolean);
Values参数是一个TStrings对象(也可以使用TStringList对象),用于指定Cookie的实际内容。Values中可以包含多个Cookie。
ADomain 参数用于限制服务器的域,Cookie只对特定域的服务器是有效的。如果这个参数为空,Cookie将被传递给任何一个服务器。一般情况下,应当把这个参数设为Web服务器的域名。
APath参数用于指定一组路径,Cookie只在特定的路径下是合法的。
AExpires参数用于指定Cookie在多长时间内是合法的,由于时差的原因,这个时间应当基于GMT时区。如果您想使Cookie的有效时间是10天,就应当把这个参数设为Now+10。如果要使一个Cookie无效,这个参数可以设为过去的一个时间。注意,一个Cookie无效后,就不会被传递给服务器,但并不从客户机中删除。
ASecure参数是一个boolean值,如果这个参数设为True,表示Cookie只能通过HTTP或SSL传递。一般情况,这个参数设为False。
在Web服务器端,您可以通过TWebRequest的CookieFields特性访问由客户传递来的Cookie。这个特性是一个TStrings对象,每个Cookie的格式是param=value。调用TWebRequest的ExtractCookieFields()方法可以把多个Cookie分解为一个个字符串。
Cookie可以保存任何字符串所能表达的信息。Cookie的长度最多可达4KB,客户的机器最多可以保存300个Cookie.每个服务器或域的Cookie不超过20个。
var
CookList : TStringList;
iBZ,fcbz:integer;
begin
CookList := TStringList.Create;
try
CookList.Add('NSRID='+ sNSRID);
CookList.Add('NSRMC=' + sNSRMC);
CookList.Add('WJBM=' + sWJBM);
CookList.Add('YHZH=' + sYHZH);
iBZ:=0;
fcbz:=0;
CookList.Add('BZ=' + IntToStr(iBZ));
CookList.Add('FCBZ=' + IntToStr(fcbz));
Response.SetCookieField(cookList,'','',Now+10,False);
finally
cookList.Free;
End;
End;//创建Cookie iBZ:=StrToInt(Request.CookieFields.Values['BZ']);
cookbuffer:array[0..20] of char
str1:string;
begin
str1:='要写的值;expires=Sun,22Feb2099 08:08:08 GMT';
StrPCopy(cookBuffer,str1);
InternetSetCookie(PChar('http://www.xxxx.com'),pchar('userName'),@cookBuffer);
end;
这样就把userName的值写进去了。