sendToControlUrl method

Future<String> sendToControlUrl(
  1. String? name,
  2. String param
)

Sends a request to the control url with the name of the action and the param. Used by Action.invoke(args)

Implementation

Future<String> sendToControlUrl(String? name, String param) async {
  final body = _soapBody.replaceAll('{param}', param);

  if (const bool.fromEnvironment('upnp.debug.control', defaultValue: false)) {
    print('Send to $controlUrl (SOAPACTION: $type#$name): $body');
  }

  final request = await UpnpCommon.httpClient.postUrl(Uri.parse(controlUrl!));
  request.headers.set('SOAPACTION', '"$type#$name"');
  request.headers.set('Content-Type', 'text/xml; charset="utf-8"');
  request.headers.set('User-Agent', 'CyberGarage-HTTP/1.0');
  // We use UTF-8 in the body so we use it here to calculate the length
  request.headers.set('Content-Length', utf8.encode(body).length);
  request.write(body);
  final response = await request.close();

  final content =
      await response.cast<List<int>>().transform(utf8.decoder).join();

  if (response.statusCode != 200) {
    try {
      final doc = XmlDocument.parse(content);
      throw UpnpException(doc.rootElement);
    } catch (e) {
      if (e is! UpnpException) {
        throw Exception('\n\n$content');
      } else {
        rethrow;
      }
    }
  } else {
    return content;
  }
}