URLSession.sessionWithConfiguration constructor
- URLSessionConfiguration config, {
- URLRequest? onRedirect(
- URLSession session,
- URLSessionTask task,
- HTTPURLResponse response,
- URLRequest newRequest,
- URLSessionResponseDisposition onResponse(
- URLSession session,
- URLSessionTask task,
- URLResponse response
- void onData(
- URLSession session,
- URLSessionTask task,
- Data error
- void onFinishedDownloading(
- URLSession session,
- URLSessionDownloadTask task,
- Uri uri
- void onComplete(
- URLSession session,
- URLSessionTask task,
- Error? error
- void onWebSocketTaskOpened(
- URLSession session,
- URLSessionWebSocketTask task,
- String? protocol
- void onWebSocketTaskClosed(
- URLSession session,
- URLSessionWebSocketTask task,
- int? closeCode,
- Data? reason,
A client with a given configuration.
If onRedirect
is set then it will be called whenever a HTTP
request returns a redirect response (e.g. 302). The response
parameter
contains the response from the server. The newRequest
parameter contains
a follow-up request that would honor the server's redirect. If the return
value of this function is null
then the redirect will not occur.
Otherwise, the returned URLRequest (usually newRequest
) will be
executed. onRedirect
will not be called for background sessions, which
automatically follow redirects. See
URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:
If onResponse
is set then it will be called whenever a valid response
is received. The returned URLSessionResponseDisposition will decide
how the content of the response is processed. See
URLSession:dataTask:didReceiveResponse:completionHandler:
If onData
is set then it will be called whenever response data is
received. If the amount of received data is large, then it may be
called more than once. See
URLSession:dataTask:didReceiveData:
If onFinishedDownloading
is set then it will be called whenever a
URLSessionDownloadTask has finished downloading.
If onComplete
is set then it will be called when a task completes. If
error
is null
then the request completed successfully. See
URLSession:task:didCompleteWithError:
See sessionWithConfiguration:delegate:delegateQueue:
If onWebSocketTaskOpened
is set then it will be called when a
URLSessionWebSocketTask successfully negotiated the handshake with the
server.
If onWebSocketTaskClosed
is set then it will be called if a
URLSessionWebSocketTask receives a close control frame from the server.
NOTE: A URLSessionWebSocketTask.receiveMessage must be in flight for
onWebSocketTaskClosed
to be called.
Implementation
factory URLSession.sessionWithConfiguration(
URLSessionConfiguration config, {
URLRequest? Function(URLSession session, URLSessionTask task,
HTTPURLResponse response, URLRequest newRequest)?
onRedirect,
URLSessionResponseDisposition Function(
URLSession session, URLSessionTask task, URLResponse response)?
onResponse,
void Function(URLSession session, URLSessionTask task, Data error)? onData,
void Function(URLSession session, URLSessionDownloadTask task, Uri uri)?
onFinishedDownloading,
void Function(URLSession session, URLSessionTask task, Error? error)?
onComplete,
void Function(
URLSession session, URLSessionWebSocketTask task, String? protocol)?
onWebSocketTaskOpened,
void Function(URLSession session, URLSessionWebSocketTask task,
int? closeCode, Data? reason)?
onWebSocketTaskClosed,
}) {
// Avoid the complexity of simultaneous or out-of-order delegate callbacks
// by only allowing callbacks to execute sequentially.
// See https://developer.apple.com/forums/thread/47252
// NOTE: this is likely to reduce throughput when there are multiple
// requests in flight because each call to a delegate waits on a lock
// that is unlocked by Dart code.
final queue = ncb.NSOperationQueue.new1(linkedLibs)
..maxConcurrentOperationCount = 1
..name =
'cupertino_http.NSURLSessionDelegateQueue'.toNSString(linkedLibs);
return URLSession._(
ncb.NSURLSession.sessionWithConfiguration_delegate_delegateQueue_(
linkedLibs, config._nsObject, _delegate, queue),
isBackground: config._isBackground,
onRedirect: onRedirect,
onResponse: onResponse,
onData: onData,
onFinishedDownloading: onFinishedDownloading,
onComplete: onComplete,
onWebSocketTaskOpened: onWebSocketTaskOpened,
onWebSocketTaskClosed: onWebSocketTaskClosed);
}