/// <summary> /// Loads the given URI by using an asynchronous request with the given /// method and body. /// </summary> /// <param name="loader">The document loader to use.</param> /// <param name="request">The request to issue.</param> /// <param name="cancel"> /// The token which can be used to cancel the request. /// </param> /// <returns> /// The task which will eventually return the response. /// </returns> public static Task <IResponse> SendAsync(this IDocumentLoader loader, DocumentRequest request, CancellationToken cancel) { if (loader == null) { return(DefaultResponse); } return(loader.LoadAsync(request, cancel)); }
async Task Receive(IDocumentLoader loader, DocumentRequest request, CancellationToken cancel) { try { using (var response = await loader.LoadAsync(request, cancel).ConfigureAwait(false)) { if (response != null) { foreach (var header in response.Headers) { _headers[header.Key] = header.Value; } _responseUrl = response.Address.Href; _responseStatus = response.StatusCode; ReadyState = RequesterState.Loading; using (var ms = new MemoryStream()) { await response.Content.CopyToAsync(ms, BufferSize, cancel).ConfigureAwait(false); ms.Seek(0, SeekOrigin.Begin); using (var reader = new StreamReader(ms)) _responseText = reader.ReadToEnd(); } Fire(LoadEndEvent); } ReadyState = RequesterState.Done; Fire(response == null ? ErrorEvent : LoadEvent); } } catch (TaskCanceledException) { ReadyState = RequesterState.Done; Fire(TimeoutEvent); } }
/// <summary> /// Loads the given URI by using an asynchronous request with the given /// method and body. /// </summary> /// <param name="loader">The document loader to use.</param> /// <param name="request">The request to issue.</param> /// <param name="cancel"> /// The token which can be used to cancel the request. /// </param> /// <returns> /// The task which will eventually return the response. /// </returns> public static Task <IResponse> SendAsync(this IDocumentLoader loader, DocumentRequest request, CancellationToken cancel) { return(loader != null?loader.LoadAsync(request, cancel) : TaskEx.FromResult(default(IResponse))); }