Sample code for 30+ languages & platforms
Xbase++

Adding Cookies to an HTTP Request

See more HTTP Examples

Demonstrates how to add one or more cookies to an HTTP request.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL oReq
LOCAL cDomain
LOCAL nPort
LOCAL nSsl
LOCAL oResp
LOCAL cHtml

nSuccess := 0

//  This example assumes the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

oHttp := CreateObject("Chilkat.Http")

//  The Cookie header field has this format:
//  Cookie: name1=value1 [; name2=value2] ...

//  Build an HTTP POST request:
oReq := CreateObject("Chilkat.HttpRequest")
oReq:SetFromUrl("http://www.chilkatsoft.com/echoPost.asp")
oReq:HttpVerb := "POST"

oReq:AddParam("param1", "value1")
oReq:AddParam("param2", "value2")

//  To add cookies to any HTTP request sent by a Chilkat HTTP method
//  that uses an HTTP request object, add the cookies to the
//  request object by calling AddHeader.  

//  Add two cookies:
oReq:AddHeader("Cookie", 'user="mary"; city="Chicago"')

//  Send the HTTP POST.  
//  (The cookies are sent as part of the HTTP header.)

cDomain := "www.chilkatsoft.com"
nPort := 80
nSsl := 0
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpSReq(cDomain, nPort, nSsl, oReq, oResp)
IF (nSuccess == 0)
    ? oHttp:LastErrorText
    oHttp:destroy()
    oReq:destroy()
    oResp:destroy()
    RETURN
ENDIF

//  Display the HTML body of the response.
IF (oResp:StatusCode == 200)
    //  Show the last HTTP request header sent, which should include
    //  our cookies...
    ? oHttp:LastHeader
ELSE
    ? "HTTP Response Status = " + Str(oResp:StatusCode)
ENDIF

? "---------------------"

//  Some Chilkat HTTP methods do not use an HTTP request object. 
//  For these methods, such as for QuickGetStr, cookies (or any HTTP request header) 
//  are added by calling SetRequestHeader.  
oHttp:SetRequestHeader("Cookie", 'user="mary"; city="Chicago"')

cHtml := oHttp:QuickGetStr("http://www.w3.org/")
IF (oHttp:LastMethodSuccess != 1)
    ? oHttp:LastErrorText
ELSE
    //  Show the last HTTP request header sent, which should include
    //  our cookies...
    ? oHttp:LastHeader
ENDIF

oHttp:destroy()
oReq:destroy()
oResp:destroy()