Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

Objective-C Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text

HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA Encryption
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(Objective-C) Adding Cookies to an HTTP Request

Demonstrates how to add one or more cookies to an HTTP request. For Chilkat HTTP methods that use an HTTP request object, such as SynchronousRequest or PostUrlEncoded, cookies are added by calling the AddHeader method on the request object. For Chilkat HTTP methods that do not use an HTTP request object, such as QuickGetStr, cookies are added by calling AddQuickHeader.

Chilkat Objective-C Library Downloads

MAC OS X (Cocoa) Libs

iOS Libs

#import "CkoHttp.h"
#import "CkoHttpRequest.h"
#import "CkoHttpResponse.h"
#import "NSString.h"



CkoHttp *http = [[CkoHttp alloc] init];

BOOL success;

//  Any string unlocks the component for the 1st 30-days.
success = [http UnlockComponent: @"Anything for 30-day trial"];
if (success != YES) {
    NSLog(@"%@",http.LastErrorText);
    return;
}

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

//  Build an HTTP POST request:
CkoHttpRequest *request = [[CkoHttpRequest alloc] init];
[request SetFromUrl: @"http://www.chilkatsoft.com/echoPost.asp"];
request.HttpVerb = @"POST";

[request AddParam: @"param1" value: @"value1"];
[request AddParam: @"param2" value: @"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:
[request AddHeader: @"Cookie" value: @"user=\"mary\"; city=\"Chicago\""];

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

CkoHttpResponse *response = 0;
NSString *domain = @"www.chilkatsoft.com";
int port = 80;
BOOL ssl = NO;
response = [http SynchronousRequest: domain port: [NSNumber numberWithInt: port] ssl: ssl req: request];
if (response == nil ) {
    NSLog(@"%@",http.LastErrorText);
    return;
}

//  Display the HTML body of the response.
if ([response.StatusCode intValue] == 200) {
    //  Show the last HTTP request header sent, which should include
    //  our cookies...
    NSLog(@"%@",http.LastHeader);
}
else {
    NSLog(@"%@%d",@"HTTP Response Status = ",[response.StatusCode intValue]);
}

NSLog(@"%@",@"---------------------");

//  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 AddQuickHeader.
success = [http AddQuickHeader: @"Cookie" value: @"user=\"mary\"; city=\"Chicago\""];

NSString *html = [http QuickGetStr: @"http://www.w3.org/"];
if (http.LastMethodSuccess != YES) {
    NSLog(@"%@",http.LastErrorText);
}
else {
    //  Show the last HTTP request header sent, which should include
    //  our cookies...
    NSLog(@"%@",http.LastHeader);
}


 

© 2000-2019 Chilkat Software, Inc. All Rights Reserved.