Use cookies in BCB

zhaozj2021-02-11  176

Cookie

First, set cookies in the active answers, and read the same cookie in the request. Set the cookies to a part of the response, use the SetCookiefIELD method:

Tstringlist * cookies = new tstringlist (); cookies-> add ("answer = 42"); // whatver ... response-> setCookiefIELD (cookies, null, null, now () 1, false); cookies-> FREE ();

Note: We use a TSTRINGList to set up a cookie value. (The first parameter passed to SetCookIEfield). Each of the cookies can have a domain (Domain) (second parameter) and path (third parameter) unite. (Like DRBOB42.COM), identify which URL is cookie to be sent. You can leave these two parameters (like the above code). The fourth parameter specifies the Cookie end date, here I set to "the same time the next time", use now () 1 to set. The last parameter is to be specified, and if the cookie is used through a secure network (Over A SECURE NETWORK).

If the user recognizes this cookie, then we need to read the value of the cookie in an onaction event. (In order to determine the value of the answer field) Cookie is part of the request class, just like ContentFields, so you can get the cookiefields property: int answer = start (Request-> cookiefs-> VALUES ["Answer"]);

Note: One exception can occur when calling strthannel () (such as a String), in this case, the cookie will not be set, no or terminate), so you should use a try-except.

Second, how to read and write cookies using VCL:

Request-> cookiefields-> Values ​​["name of cookie"]; write to a cookie:

Response-> SetCookief (Tstrings * Values, Ansistring Domain, Ansistring Apath, TdateTime Aexpires, Bool Asecure;

转载请注明原文地址:https://www.9cbs.com/read-4891.html

New Post(0)