![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(C) Example: Http.SetCookieXml methodDemonstrates the
#include <C_CkHttp.h> #include <C_CkStringBuilder.h> void ChilkatSample(void) { BOOL success; HCkHttp http; const char *html; HCkHttp httpB; HCkHttp httpC; HCkStringBuilder sbCookiesXml; success = FALSE; http = CkHttp_Create(); CkHttp_putSaveCookies(http,TRUE); CkHttp_putCookieDir(http,"c:/temp/cookie_cache"); // For this initial request, we are not sending cookies. // We are starting a new (cookie) session and saving the cookies we receive. CkHttp_putSendCookies(http,FALSE); // Do a request that establishes and saves cookies in files in the cookie directory. html = CkHttp_quickGetStr(http,"https://google.com/"); // Examine the LastResponseHeader to see the cookies that were received: printf("%s\n",CkHttp_lastResponseHeader(http)); printf("----------------------------------\n"); // We can see the following Set-Cookie response header fields: // Set-Cookie: AEC=AVh_V2h-fsL2-****; expires=Mon, 23-Feb-2026 19:15:10 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=lax // Set-Cookie: NID=525=XC_cL****Hn7-WONX; expires=Thu, 26-Feb-2026 19:15:10 GMT; path=/; domain=.google.com; HttpOnly // The following file was created: c:/temp/cookie_cache/google_com.xml // Examine the cookies received in the above request. printf("%s\n",CkHttp_getCookieXml(http,"google.com")); printf("----------------------------------\n"); // Sample output. // <?xml version="1.0" encoding="utf-8"?> // <cookies> // <cookie key=".google.com,/,AEC" v="0" expire="Mon, 23-Feb-2026 19:05:20 GMT" secure="yes"> // <AEC>AVh_V2gL8QzTdFGYq6_rS6ktBfqm8WNG3pzHxS2nTZD5i23dRBau2c4ZRA</AEC> // </cookie> // <cookie key=".google.com,/,NID" v="0" expire="Thu, 26-Feb-2026 19:05:20 GMT"> // <NID>525=SuLcnaSkFqF4Jz_jLEq4kt_f3MY2Xro1VDoVzLKvp8XHcW2UHuLKJSr55iDeW0NiRIPXoAwJWF1-YNl29unX2xfhEWsS5BhbuK_2DXdD9cTOmn5BSENMhZasxeJ71mEP2PQRMXBndqnl41DhblC2jjdac_so4TESIll1B0GCVe9wRFjqI6DTZItRCj61BHmr1_RAQi0_jrh_ihn6KYtIFEY7</NID> // </cookie> // </cookies> // ------------------------------------------------------------------------------------------- // Let's say we want to continue with the session at some later time with a new HTTP object. // One way to do it is to simply set the same cookie properties: httpB = CkHttp_Create(); CkHttp_putSaveCookies(httpB,TRUE); CkHttp_putCookieDir(httpB,"c:/temp/cookie_cache"); CkHttp_putSendCookies(httpB,TRUE); // The cookies from the cache are sent. html = CkHttp_quickGetStr(httpB,"https://google.com/"); // Examine the LastHeader to see the contents of the Cookie header field. printf("%s\n",CkHttp_lastHeader(httpB)); printf("----------------------------------\n"); // ------------------------------------------------------------------------------------------- // Another way to do it is to explicitly load the cookies from XML // and set the cookies for the particular domain. httpC = CkHttp_Create(); CkHttp_putSaveCookies(httpC,TRUE); // Do not save cookies to files, just keep them in memory. CkHttp_putCookieDir(httpC,"memory"); CkHttp_putSendCookies(httpC,TRUE); sbCookiesXml = CkStringBuilder_Create(); success = CkStringBuilder_LoadFile(sbCookiesXml,"c:/temp/cookie_cache/google_com.xml","utf-8"); if (success == FALSE) { printf("%s\n",CkStringBuilder_lastErrorText(sbCookiesXml)); CkHttp_Dispose(http); CkHttp_Dispose(httpB); CkHttp_Dispose(httpC); CkStringBuilder_Dispose(sbCookiesXml); return; } success = CkHttp_SetCookieXml(httpC,"google.com",CkStringBuilder_getAsString(sbCookiesXml)); if (success == FALSE) { printf("%s\n",CkHttp_lastErrorText(httpC)); CkHttp_Dispose(http); CkHttp_Dispose(httpB); CkHttp_Dispose(httpC); CkStringBuilder_Dispose(sbCookiesXml); return; } html = CkHttp_quickGetStr(httpC,"https://google.com/"); // Examine the LastHeader to see the contents of the Cookie header field. printf("%s\n",CkHttp_lastHeader(httpC)); CkHttp_Dispose(http); CkHttp_Dispose(httpB); CkHttp_Dispose(httpC); CkStringBuilder_Dispose(sbCookiesXml); } |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.