105

This method submits a simple HTTP request and calls a success or error callback just fine:

 void _getSimpleReply( String command, callback, errorCallback ) async { try { HttpClientRequest request = await _myClient.get( _serverIPAddress, _serverPort, '/' ); HttpClientResponse response = await request.close(); response.transform( utf8.decoder ).listen( (onData) { callback( onData ); } ); } on SocketException catch( e ) { errorCallback( e.toString() ); } } 

If the server isn't running, the Android-app more or less instantly calls the errorCallback.

On iOS, the errorCallback takes a very long period of time - more than 20 seconds - until any callback gets called.

May I set for HttpClient() a maximum number of seconds to wait for the server side to return a reply - if any?

6 Answers 6

135

There are two different ways to configure this behavior in Dart

Set a per request timeout

You can set a timeout on any Future using the Future.timeout method. This will short-circuit after the given duration has elapsed by throwing a TimeoutException.

try { final request = await client.get(...); final response = await request.close() .timeout(const Duration(seconds: 2)); // rest of the code ... } on TimeoutException catch (_) { // A timeout occurred. } on SocketException catch (_) { // Other exception } 

Set a timeout on HttpClient

You can also set a timeout on the HttpClient itself using HttpClient.connectionTimeout. This will apply to all requests made by the same client, after the timeout was set. When a request exceeds this timeout, a SocketException is thrown.

final client = new HttpClient(); client.connectionTimeout = const Duration(seconds: 5); 
Sign up to request clarification or add additional context in comments.

5 Comments

Connection timeout and request timeout are two different concepts. These two ways specified in this answer are not interchangeable and each one does a different thing.
Hi, about request.close().timeout(), I am afraid it will waste resource: if the http request last for, say, 20s, then even if it "timeouts", the resource is still be used! How can I solve it?
For any future readers, see @ch271828n's question: Proper way of setting request timeout for Flutter http requests?.
Why i always got The method 'close' isn't defined for the type 'Response'., on .close() method?
@Jonah, do u know if the android has the max timeout to 30sec in its OS? I have a download URL file that exceed the 30s. With IOs it's ok, however android is giving me status 503 in 30s
105

You can use timeout

http.get(Uri.parse('url')).timeout( const Duration(seconds: 1), onTimeout: () { // Time has run out, do what you wanted to do. return http.Response('Error', 408); // Request Timeout response status code }, ); 

4 Comments

This worked fine. Just in case you are using this you can not return null. Use something like this "return http.Response('', 500);" replace 500 for any http code you need
Hi, about request.timeout(), I am afraid it will waste resource: if the http request last for, say, 20s, then even if it "timeouts", the resource is still be used! How can I solve it?
This answer probably should clarify that it's using package:http and not dart:io's HttpClient.
How about setting the http status code to 408?
5

The HttpClient.connectionTimeout didn't work for me. However, I knew that the Dio packet allows request cancellation. Then, I dig into the packet to find out how they achieve it and I adapted it to me. What I did was to create two futures:

  • A Future.delayed where I set the duration of the timeout.
  • The HTTP request.

Then, I passed the two futures to a Future.any which returns the result of the first future to complete and the results of all the other futures are discarded. Therefore, if the timeout future completes first, your connection times out and no response will arrive. You can check it out in the following code:

Future<Response> get( String url, { Duration timeout = Duration(seconds: 30), }) async { final request = Request('GET', Uri.parse(url))..followRedirects = false; headers.forEach((key, value) { request.headers[key] = value; }); final Completer _completer = Completer(); /// Fake timeout by forcing the request future to complete if the duration /// ends before the response arrives. Future.delayed(timeout, () => _completer.complete()); final response = await Response.fromStream(await listenCancelForAsyncTask( _completer, Future(() { return _getClient().send(request); }), )); } Future<T> listenCancelForAsyncTask<T>( Completer completer, Future<T> future, ) { /// Returns the first future of the futures list to complete. Therefore, /// if the first future is the timeout, the http response will not arrive /// and it is possible to handle the timeout. return Future.any([ if (completer != null) completeFuture(completer), future, ]); } Future<T> completeFuture<T>(Completer completer) async { await completer.future; throw TimeoutException('TimeoutError'); }

Comments

2

You can also choose to override the settings for a HttpClient:

class DevHttpOverrides extends HttpOverrides { @override HttpClient createHttpClient(SecurityContext? context) { return super.createHttpClient(context) ..connectionTimeout = Duration(seconds: 2); } } 

Comments

1

This is an example of how to extend the http.BaseClient class to support timeout and ignore the exception of the S.O. if the client's timeout is reached first. you just need to override the "send" method...

the timeout should be passed as a parameter to the class constructor.

import 'dart:async'; import 'package:http/http.dart' as http; // as dart does not support tuples i create an Either class class _Either<L, R> { final L? left; final R? right; _Either(this.left, this.right); _Either.Left(L this.left) : right = null; _Either.Right(R this.right) : left = null; } class TimeoutClient extends http.BaseClient { final http.Client _httpClient; final Duration timeout; TimeoutClient( {http.Client? httpClient, this.timeout = const Duration(seconds: 30)}) : _httpClient = httpClient ?? http.Client(); Future<http.StreamedResponse> send(http.BaseRequest request) async { // wait for result between two Futures (the one that is reached first) in silent mode (no throw exception) _Either<http.StreamedResponse, Exception> result = await Future.any([ Future.delayed( timeout, () => _Either.Right( TimeoutException( 'Client connection timeout after ${timeout.inMilliseconds} ms.'), )), Future(() async { try { return _Either.Left(await _httpClient.send(request)); } on Exception catch (e) { return _Either.Right(e); } }) ]); // this code is reached only for first Future response, // the second Future is ignorated and does not reach this point if (result.right != null) { throw result.right!; } return result.left!; } } 

Comments

1

Their is onError option which works fine if their is any exception like no internet.It has to return response(my case in below code) or null. In response their are 2 options Body and Status code.

var response = await http.post(url, body: body, headers: _headers).onError( (error, stackTrace) => http.Response( jsonEncode({ 'message':no internet please connect to internet first }), 408)); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.