IOClient class
A dart:io-based HTTP client. This is the default client.
class IOClient extends BaseClient {
/// The underlying `dart:io` HTTP client.
HttpClient _inner;
/// Creates a new HTTP client.
IOClient() : _inner = new HttpClient();
/// Sends an HTTP request and asynchronously returns the response.
Future<StreamedResponse> send(BaseRequest request) {
var stream = request.finalize();
return _inner.openUrl(request.method, request.url).then((ioRequest) {
ioRequest.followRedirects = request.followRedirects;
ioRequest.maxRedirects = request.maxRedirects;
ioRequest.contentLength = request.contentLength;
ioRequest.persistentConnection = request.persistentConnection;
request.headers.forEach((name, value) {
ioRequest.headers.set(name, value);
});
return stream.pipe(ioRequest);
}).then((response) {
var headers = {};
response.headers.forEach((key, values) {
headers[key] = values.join(',');
});
return new StreamedResponse(
response,
response.statusCode,
response.contentLength,
request: request,
headers: headers,
isRedirect: response.isRedirect,
persistentConnection: response.persistentConnection,
reasonPhrase: response.reasonPhrase);
});
}
/// Closes the client. This terminates all active connections. If a client
/// remains unclosed, the Dart process may not terminate.
void close() {
if (_inner != null) _inner.close(force: true);
_inner = null;
}
}
Extends
BaseClient > IOClient
Constructors
new IOClient() #
Creates a new HTTP client.
IOClient() : _inner = new HttpClient();
Methods
void close() #
Closes the client. This terminates all active connections. If a client remains unclosed, the Dart process may not terminate.
void close() {
if (_inner != null) _inner.close(force: true);
_inner = null;
}
Future<Response> post(url, {Map<String, String> headers, Map<String, String> fields}) #
Sends an HTTP POST request with the given headers and fields to the given
URL, which can be a Uri or a String. If any fields are specified, the
content-type is automatically set to
"application/x-www-form-urlencoded".
For more fine-grained control over the request, use send instead.
Future<Response> post(url,
{Map<String, String> headers,
Map<String, String> fields}) =>
_sendUnstreamed("POST", url, headers, fields);
Future<Response> put(url, {Map<String, String> headers, Map<String, String> fields}) #
Sends an HTTP PUT request with the given headers and fields to the given
URL, which can be a Uri or a String. If any fields are specified, the
content-type is automatically set to
"application/x-www-form-urlencoded".
For more fine-grained control over the request, use send instead.
Future<Response> put(url,
{Map<String, String> headers,
Map<String, String> fields}) =>
_sendUnstreamed("PUT", url, headers, fields);
Future<String> read(url, {Map<String, String> headers}) #
Sends an HTTP GET request with the given headers to the given URL, which can be a Uri or a String, and returns a Future that completes to the body of the response as a String.
The Future will emit an HttpException if the response doesn't have a success status code.
For more fine-grained control over the request and response, use send or get instead.
Future<String> read(url, {Map<String, String> headers}) {
return get(url, headers: headers).then((response) {
_checkResponseSuccess(url, response);
return response.body;
});
}
Future<Uint8List> readBytes(url, {Map<String, String> headers}) #
Sends an HTTP GET request with the given headers to the given URL, which can be a Uri or a String, and returns a Future that completes to the body of the response as a list of bytes.
The Future will emit an HttpException if the response doesn't have a success status code.
For more fine-grained control over the request and response, use send or get instead.
Future<Uint8List> readBytes(url, {Map<String, String> headers}) {
return get(url, headers: headers).then((response) {
_checkResponseSuccess(url, response);
return response.bodyBytes;
});
}
Future<StreamedResponse> send(BaseRequest request) #
Sends an HTTP request and asynchronously returns the response.
Future<StreamedResponse> send(BaseRequest request) {
var stream = request.finalize();
return _inner.openUrl(request.method, request.url).then((ioRequest) {
ioRequest.followRedirects = request.followRedirects;
ioRequest.maxRedirects = request.maxRedirects;
ioRequest.contentLength = request.contentLength;
ioRequest.persistentConnection = request.persistentConnection;
request.headers.forEach((name, value) {
ioRequest.headers.set(name, value);
});
return stream.pipe(ioRequest);
}).then((response) {
var headers = {};
response.headers.forEach((key, values) {
headers[key] = values.join(',');
});
return new StreamedResponse(
response,
response.statusCode,
response.contentLength,
request: request,
headers: headers,
isRedirect: response.isRedirect,
persistentConnection: response.persistentConnection,
reasonPhrase: response.reasonPhrase);
});
}