Dart
Dart
Client Certificate in REST (USB Token or Smartcard)
See more REST Examples
Demonstrates how to use a client certificate with a REST connection where the certificate and private key are located on a USB token or smart card.Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// This example shows how to use the Chilkat socket object's connection.
final rest = CkRest();
final socket = CkSocket();
// Set the certificate to be used for mutual TLS authentication
final cert = CkCert();
// If the smartcard or token requires a PIN...
cert.smartCardPin = '000000';
try {
cert.loadFromSmartcard('');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
try {
socket.setSslClientCert(cert);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Establish the connection using the socket object (with client certificate authentication).
final bTls = true;
final port = 443;
final maxWaitMs = 5000;
try {
socket.connect('www.example.com', port, bTls, maxWaitMs);
} on ChilkatException catch (e) {
print('Connect Failure Error Code: ${socket.connectFailReason}');
print(e.lastErrorText);
return;
}
final bAutoReconnect = true;
// Use the connection:
try {
rest.useConnection(socket, bAutoReconnect);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// At this point we are connected and can make REST calls...
// For example..
final String responseJson;
try {
responseJson = rest.fullRequestNoBody('GET', '/someQuery');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
}