UrlConnection class abstract

Represents a communication link between the application and a Url.

This class serves as the base abstraction for different types of connections (e.g., HTTP, FTP). Concrete subclasses are expected to implement protocol-specific connection logic in the connect method.

Once connected, the UrlConnection provides access to the request and response objects, and allows obtaining an input stream for reading the response body.

Example

class HttpUrlConnection extends UrlConnection {
  HttpUrlConnection(Url url) : super(url);

  @override
  Future<void> connect() async {
    final client = HttpClient();
    final req = await client.getUrl(Uri.parse(url.toString()));
    _setRequest(req);
    _setResponse(await req.close());
  }
}

final connection = HttpUrlConnection(Url.parse('https://example.com'));
await connection.connect();
final stream = connection.getInputStream();
int byte;
while ((byte = await stream.readByte()) != -1) {
  print(byte);
}
Implementers

Constructors

UrlConnection(Url url)
Represents a communication link between the application and a Url.

Properties

hashCode int
The hash code for this object.
no setterinherited
request HttpClientRequest?
Returns the underlying HttpClientRequest object.
no setter
response HttpClientResponse?
Returns the underlying HttpClientResponse object.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
url Url
The Url this connection was created for.
final

Methods

connect() Future<void>
Establishes a connection to the resource referenced by this UrlConnection.
getInputStream() InputStream
Returns an InputStream that reads the response body of this connection.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited