Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Objective-C Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
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
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Objective-C) HTTP request for a SOAP web service using WS-Security 1.0 with a digital certificate for authentication

See more HTTP Examples

Demonstrates how to build and send an HTTP request for a SOAP web service using WS-Security 1.0 with a digital certificate for authentication.

Chilkat Objective-C Library Downloads

MAC OS X (Cocoa) Libs

iOS Libs

#import <CkoCert.h>
#import <CkoXml.h>
#import <CkoXmlDSigGen.h>
#import <CkoStringBuilder.h>
#import <CkoHttp.h>
#import <CkoHttpResponse.h>

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

// 

// ------------------------------------
// Step 1: Load the signing certificate
// ------------------------------------
CkoCert *cert = [[CkoCert alloc] init];
BOOL success = [cert LoadFromSmartcard: @""];
if (success == NO) {
    NSLog(@"%@",cert.LastErrorText);
    return;
}

// ---------------------------------------
// Step 2: Build the SOAP XML to be signed
// ---------------------------------------

CkoXml *xml = [[CkoXml alloc] init];
xml.Tag = @"SOAP-ENV:Envelope";
[xml AddAttribute: @"xmlns:SOAP-ENV" value: @"http://schemas.xmlsoap.org/soap/envelope/"];
[xml AddAttribute: @"xmlns:web" value: @"http://www.example.com/webservice/"];
[xml UpdateAttrAt: @"SOAP-ENV:Header|wsse:Security" autoCreate: YES attrName: @"xmlns:ds" attrValue: @"http://www.w3.org/2000/09/xmldsig#"];
[xml UpdateAttrAt: @"SOAP-ENV:Header|wsse:Security" autoCreate: YES attrName: @"xmlns:wsse" attrValue: @"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"];
[xml UpdateAttrAt: @"SOAP-ENV:Header|wsse:Security" autoCreate: YES attrName: @"xmlns:wsu" attrValue: @"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"];
[xml UpdateAttrAt: @"SOAP-ENV:Header|wsse:Security" autoCreate: YES attrName: @"xmlns:xenc" attrValue: @"http://www.w3.org/2001/04/xmlenc#"];
[xml UpdateAttrAt: @"SOAP-ENV:Header|wsse:Security" autoCreate: YES attrName: @"SOAP-ENV:mustUnderstand" attrValue: @"1"];
[xml UpdateAttrAt: @"SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken" autoCreate: YES attrName: @"A1" attrValue: @""];
[xml UpdateAttrAt: @"SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken" autoCreate: YES attrName: @"EncodingType" attrValue: @"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"];
[xml UpdateAttrAt: @"SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken" autoCreate: YES attrName: @"ValueType" attrValue: @"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509"];
[xml UpdateAttrAt: @"SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken" autoCreate: YES attrName: @"wsu:Id" attrValue: @"x509cert00"];
[xml UpdateChildContent: @"SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken" value: [cert GetEncoded]];
[xml UpdateAttrAt: @"SOAP-ENV:Body" autoCreate: YES attrName: @"xmlns:wsu" attrValue: @"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"];
[xml UpdateAttrAt: @"SOAP-ENV:Body" autoCreate: YES attrName: @"wsu:Id" attrValue: @"TheBody"];
[xml UpdateChildContent: @"SOAP-ENV:Body|web:MyRequest|web:Parameter" value: @"MyValue"];

// ---------------------------------------
// Step 3: Sign the XML
// ---------------------------------------

CkoXmlDSigGen *gen = [[CkoXmlDSigGen alloc] init];

gen.SigLocation = @"SOAP-ENV:Envelope|SOAP-ENV:Header|wsse:Security";
gen.SigLocationMod = [NSNumber numberWithInt:0];
gen.SigNamespacePrefix = @"ds";
gen.SigNamespaceUri = @"http://www.w3.org/2000/09/xmldsig#";
gen.SignedInfoPrefixList = @"ds wsu xenc SOAP-ENV ";
gen.IncNamespacePrefix = @"c14n";
gen.IncNamespaceUri = @"http://www.w3.org/2001/10/xml-exc-c14n#";
gen.SignedInfoCanonAlg = @"EXCL_C14N";
gen.SignedInfoDigestMethod = @"sha1";

// -------- Reference 1 --------
[gen AddSameDocRef: @"TheBody" digestMethod: @"sha1" canonMethod: @"EXCL_C14N" prefixList: @"wsu SOAP-ENV" refType: @""];

[gen SetX509Cert: cert usePrivateKey: YES];

gen.KeyInfoType = @"Custom";

// Create the custom KeyInfo XML..
CkoXml *xmlCustomKeyInfo = [[CkoXml alloc] init];
xmlCustomKeyInfo.Tag = @"wsse:SecurityTokenReference";
xmlCustomKeyInfo.Content = @"5";
[xmlCustomKeyInfo UpdateAttrAt: @"wsse:Reference" autoCreate: YES attrName: @"URI" attrValue: @"#x509cert00"];
[xmlCustomKeyInfo UpdateAttrAt: @"wsse:Reference" autoCreate: YES attrName: @"ValueType" attrValue: @"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509"];

xmlCustomKeyInfo.EmitXmlDecl = NO;
gen.CustomKeyInfoXml = [xmlCustomKeyInfo GetXml];

// Load XML to be signed...
CkoStringBuilder *sbXml = [[CkoStringBuilder alloc] init];
xml.EmitXmlDecl = NO;
[xml GetXmlSb: sbXml];

gen.Behaviors = @"IndentedSignature";

// Sign the XML...
success = [gen CreateXmlDSigSb: sbXml];
if (success != YES) {
    NSLog(@"%@",gen.LastErrorText);
    return;
}

// ---------------------------------------------------------
// Step 4: Send the HTTP POST containing the Signed SOAP XML
// ---------------------------------------------------------

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

[http SetRequestHeader: @"SOAPAction" value: @"\"http://www.example.com/MyWebService\""];
[http SetRequestHeader: @"Host" value: @"www.example.com"];
[http SetRequestHeader: @"Content-Type" value: @"text/xml; charset=utf-8"];

CkoHttpResponse *resp = [http PTextSb: @"POST" url: @"https://example.com/MyWebService" textData: sbXml charset: @"utf-8" contentType: @"text/xml; charset=utf-8" md5: NO gzip: NO];
if (http.LastMethodSuccess == NO) {
    NSLog(@"%@",http.LastErrorText);
    return;
}

NSLog(@"%d",[resp.StatusCode intValue]);
NSLog(@"%@",resp.BodyStr);

NSLog(@"%@",@"Finished.");
 

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