/// <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 cancellation token.</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) { var download = loader.DownloadAsync(request); cancel.Register(download.Cancel); return(download.Task); } return(TaskEx.FromResult(default(IResponse))); }
async Task Receive(IDocumentLoader loader, DocumentRequest request) { var eventName = ErrorEvent; try { _download = loader.DownloadAsync(request); using (var response = await _download.Task.ConfigureAwait(false)) { if (response != null) { eventName = LoadEvent; 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).ConfigureAwait(false); ms.Seek(0, SeekOrigin.Begin); using (var reader = new StreamReader(ms)) { _responseText = reader.ReadToEnd(); } } Fire(LoadEndEvent); } ReadyState = RequesterState.Done; Fire(eventName); } } catch (TaskCanceledException) { ReadyState = RequesterState.Done; Fire(TimeoutEvent); } }
async Task Receive(IDocumentLoader loader, DocumentRequest request, CancellationToken cancel) { try { using (var response = await loader.DownloadAsync(request).Task.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); } }