UrlConnection constructor
UrlConnection(
- Url url
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);
}
Implementation
UrlConnection(this.url);