Tcl
Tcl
Adding Cookies to an HTTP Request
See more HTTP Examples
Demonstrates how to add one or more cookies to an HTTP request.Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
# This example assumes the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.
set http [new_CkHttp]
# The Cookie header field has this format:
# Cookie: name1=value1 [; name2=value2] ...
# Build an HTTP POST request:
set req [new_CkHttpRequest]
CkHttpRequest_SetFromUrl $req "http://www.chilkatsoft.com/echoPost.asp"
CkHttpRequest_put_HttpVerb $req "POST"
CkHttpRequest_AddParam $req "param1" "value1"
CkHttpRequest_AddParam $req "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:
CkHttpRequest_AddHeader $req "Cookie" "user=\"mary\"; city=\"Chicago\""
# Send the HTTP POST.
# (The cookies are sent as part of the HTTP header.)
set domain "www.chilkatsoft.com"
set port 80
set ssl 0
set resp [new_CkHttpResponse]
set success [CkHttp_HttpSReq $http $domain $port $ssl $req $resp]
if {$success == 0} then {
puts [CkHttp_lastErrorText $http]
delete_CkHttp $http
delete_CkHttpRequest $req
delete_CkHttpResponse $resp
exit
}
# Display the HTML body of the response.
if {[CkHttpResponse_get_StatusCode $resp] == 200} then {
# Show the last HTTP request header sent, which should include
# our cookies...
puts [CkHttp_lastHeader $http]
} else {
puts "HTTP Response Status = [CkHttpResponse_get_StatusCode $resp]"
}
puts "---------------------"
# 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.
CkHttp_SetRequestHeader $http "Cookie" "user=\"mary\"; city=\"Chicago\""
set html [CkHttp_quickGetStr $http "http://www.w3.org/"]
if {[CkHttp_get_LastMethodSuccess $http] != 1} then {
puts [CkHttp_lastErrorText $http]
} else {
# Show the last HTTP request header sent, which should include
# our cookies...
puts [CkHttp_lastHeader $http]
}
delete_CkHttp $http
delete_CkHttpRequest $req
delete_CkHttpResponse $resp