Sample code for 30+ languages & platforms
Unicode C

Example: Http.SetCookieXml method

Demonstrates the SetCookieXml method.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    const wchar_t *html;
    HCkHttpW httpB;
    HCkHttpW httpC;
    HCkStringBuilderW sbCookiesXml;

    success = FALSE;

    http = CkHttpW_Create();

    CkHttpW_putSaveCookies(http,TRUE);
    CkHttpW_putCookieDir(http,L"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.
    CkHttpW_putSendCookies(http,FALSE);

    //  Do a request that establishes and saves cookies in files in the cookie directory.
    html = CkHttpW_quickGetStr(http,L"https://google.com/");

    //  Examine the LastResponseHeader to see the cookies that were received:
    wprintf(L"%s\n",CkHttpW_lastResponseHeader(http));
    wprintf(L"----------------------------------\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.
    wprintf(L"%s\n",CkHttpW_getCookieXml(http,L"google.com"));
    wprintf(L"----------------------------------\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 = CkHttpW_Create();

    CkHttpW_putSaveCookies(httpB,TRUE);
    CkHttpW_putCookieDir(httpB,L"c:/temp/cookie_cache");
    CkHttpW_putSendCookies(httpB,TRUE);

    //  The cookies from the cache are sent.
    html = CkHttpW_quickGetStr(httpB,L"https://google.com/");

    //  Examine the LastHeader to see the contents of the Cookie header field.
    wprintf(L"%s\n",CkHttpW_lastHeader(httpB));
    wprintf(L"----------------------------------\n");

    //  -------------------------------------------------------------------------------------------
    //  Another way to do it is to explicitly load the cookies from XML
    //  and set the cookies for the particular domain.

    httpC = CkHttpW_Create();

    CkHttpW_putSaveCookies(httpC,TRUE);
    //  Do not save cookies to files, just keep them in memory.
    CkHttpW_putCookieDir(httpC,L"memory");
    CkHttpW_putSendCookies(httpC,TRUE);

    sbCookiesXml = CkStringBuilderW_Create();
    success = CkStringBuilderW_LoadFile(sbCookiesXml,L"c:/temp/cookie_cache/google_com.xml",L"utf-8");
    if (success == FALSE) {
        wprintf(L"%s\n",CkStringBuilderW_lastErrorText(sbCookiesXml));
        CkHttpW_Dispose(http);
        CkHttpW_Dispose(httpB);
        CkHttpW_Dispose(httpC);
        CkStringBuilderW_Dispose(sbCookiesXml);
        return;
    }

    success = CkHttpW_SetCookieXml(httpC,L"google.com",CkStringBuilderW_getAsString(sbCookiesXml));
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(httpC));
        CkHttpW_Dispose(http);
        CkHttpW_Dispose(httpB);
        CkHttpW_Dispose(httpC);
        CkStringBuilderW_Dispose(sbCookiesXml);
        return;
    }

    html = CkHttpW_quickGetStr(httpC,L"https://google.com/");

    //  Examine the LastHeader to see the contents of the Cookie header field.
    wprintf(L"%s\n",CkHttpW_lastHeader(httpC));


    CkHttpW_Dispose(http);
    CkHttpW_Dispose(httpB);
    CkHttpW_Dispose(httpC);
    CkStringBuilderW_Dispose(sbCookiesXml);

    }