Sample code for 30+ languages & platforms
Dart

Use an Existing Socket Connection for REST

See more REST Examples

Demonstrates Rest.UseConnection, which supplies an already-connected Socket as the transport for REST requests instead of calling Connect. This is useful when the connection must be established with specific socket options or shared across objects.

Background. Rest can operate over any connected socket. The second argument enables automatic reconnection so a dropped connection is transparently re-established between requests.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // Demonstrates supplying an already-connected socket for the REST transport.  This is useful when
  // the connection must be established with specific socket options, or reused across objects.

  final socket = CkSocket();
  final bSsl = true;
  try {
    socket.connect('example.com', 443, bSsl, 5000);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final rest = CkRest();

  // Use the connected socket as the transport.  The 2nd argument enables automatic reconnection.
  final bAutoReconnect = true;
  try {
    rest.useConnection(socket, bAutoReconnect);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final String responseText;
  try {
    responseText = rest.fullRequestNoBody('GET', '/api/status');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print(responseText);
}