/// <summary> /// Begins the read response string. /// </summary> /// <param name="callback">The callback.</param> /// <param name="state">The state.</param> /// <returns></returns> public Task <string> ReadResponseStringAsync() { return(WaitForTask.ContinueWith(_ => webRequest .GetResponseAsync() .ContinueWith(t => ReadStringInternal(() => t.Result)) .ContinueWith(task => RetryIfNeedTo(task, ReadResponseStringAsync)) .Unwrap()) .Unwrap()); }
public Task <byte[]> ReadResponseBytesAsync() { return(WaitForTask.ContinueWith(_ => webRequest .GetResponseAsync() .ContinueWith(t => ReadResponse(() => t.Result, ConvertStreamToBytes)) .ContinueWith(task => RetryIfNeedTo(task, ReadResponseBytesAsync)) .Unwrap()) .Unwrap()); }
public Task <byte[]> ReadResponseBytesAsync() { return(WaitForTask.ContinueWith(_ => httpClient.SendAsync(new HttpRequestMessage(method, url)) .ConvertSecurityExceptionToServerNotFound() .AddUrlIfFaulting(url) .ContinueWith(t => ReadResponse(() => t.Result, ConvertStreamToBytes)) .ContinueWith(task => RetryIfNeedTo(task, ReadResponseBytesAsync)) .Unwrap()) .Unwrap()); }
public Task <byte[]> ReadResponseBytesAsync() { return(WaitForTask.ContinueWith(_ => webRequest .GetResponseAsync() .ConvertSecurityExceptionToServerNotFound() .AddUrlIfFaulting(webRequest.RequestUri) .ContinueWith(t => ReadResponse(() => t.Result, ConvertStreamToBytes)) .ContinueWith(task => RetryIfNeedTo(task, ReadResponseBytesAsync)) .Unwrap()) .Unwrap()); }
/// <summary> /// Begins the read response string. /// </summary> private Task <string> ReadResponseStringAsync() { return(WaitForTask.ContinueWith(_ => webRequest .GetResponseAsync() .ConvertSecurityExceptionToServerNotFound() .AddUrlIfFaulting(webRequest.RequestUri) .ContinueWith(t => ReadStringInternal(() => t.Result)) .ContinueWith(task => RetryIfNeedTo(task, ReadResponseStringAsync)) .Unwrap()) .Unwrap()); }
/// <summary> /// Begins the read response string. /// </summary> private Task <string> ReadResponseStringAsync() { var requestMessage = new HttpRequestMessage(method, url); return(WaitForTask.ContinueWith(_ => httpClient.SendAsync(requestMessage) .ConvertSecurityExceptionToServerNotFound() .AddUrlIfFaulting(url) .ContinueWith(t => ReadStringInternal(() => t.Result)) .ContinueWith(task => RetryIfNeedTo(task, ReadResponseStringAsync)) .Unwrap()) .Unwrap()); }
/// <summary> /// Begins the write operation /// </summary> public Task WriteAsync(byte[] byteArray) { postedData = byteArray; return(WaitForTask.ContinueWith(_ => webRequest.GetRequestStreamAsync().ContinueWith(t => { var dataStream = new GZipStream(t.Result, CompressionMode.Compress); using (dataStream) { dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); } })).Unwrap()); }
public Task <WebResponse> RawExecuteRequestAsync() { if (requestSendToServer) { throw new InvalidOperationException("Request was already sent to the server, cannot retry request."); } requestSendToServer = true; webRequest.AllowReadStreamBuffering = false; webRequest.AllowWriteStreamBuffering = false; return(WaitForTask.ContinueWith(_ => webRequest .GetResponseAsync() .ConvertSecurityExceptionToServerNotFound() .AddUrlIfFaulting(webRequest.RequestUri)) .Unwrap()); }
/// <summary> /// Begins the read response string. /// </summary> private Task <string> ReadResponseStringAsync() { if (requestSendToServer) { throw new InvalidOperationException("Request was already sent to the server, cannot retry request."); } requestSendToServer = true; return(WaitForTask.ContinueWith(_ => webRequest .GetResponseAsync() .ConvertSecurityExceptionToServerNotFound() .AddUrlIfFaulting(webRequest.RequestUri) .ContinueWith(t => ReadStringInternal(() => t.Result)) .ContinueWith(task => RetryIfNeedTo(task, ReadResponseStringAsync)) .Unwrap()) .Unwrap()); }
/// <summary> /// Begins the write operation /// </summary> public Task WriteAsync(string data) { return(WaitForTask.ContinueWith(_ => webRequest.GetRequestStreamAsync() .ContinueWith(task => { Stream dataStream = factory.DisableRequestCompression == false ? new GZipStream(task.Result, CompressionMode.Compress) : task.Result; var streamWriter = new StreamWriter(dataStream, Encoding.UTF8); return streamWriter.WriteAsync(data) .ContinueWith(writeTask => { streamWriter.Dispose(); dataStream.Dispose(); task.Result.Dispose(); return writeTask; }).Unwrap(); }).Unwrap()) .Unwrap()); }
public Task <IObservable <string> > ServerPullAsync(int retries = 0) { return(WaitForTask.ContinueWith(__ => { webRequest.AllowReadStreamBuffering = false; webRequest.AllowWriteStreamBuffering = false; webRequest.Headers["Requires-Big-Initial-Download"] = "True"; return webRequest.GetResponseAsync() .ContinueWith(task => { var stream = task.Result.GetResponseStream(); var observableLineStream = new ObservableLineStream(stream, () => { webRequest.Abort(); try { task.Result.Close(); } catch (Exception) { // we expect an exception, because we aborted the connection } }); observableLineStream.Start(); return (IObservable <string>)observableLineStream; }) .ContinueWith(task => { var webException = task.Exception.ExtractSingleInnerException() as WebException; if (webException == null || retries >= 3 || disabledAuthRetries) { return task; // effectively throw } var httpWebResponse = webException.Response as HttpWebResponse; if (httpWebResponse == null || (httpWebResponse.StatusCode != HttpStatusCode.Unauthorized && httpWebResponse.StatusCode != HttpStatusCode.Forbidden && httpWebResponse.StatusCode != HttpStatusCode.PreconditionFailed)) { return task; // effectively throw } if (httpWebResponse.StatusCode == HttpStatusCode.Forbidden) { HandleForbiddenResponseAsync(httpWebResponse); return task; } var authorizeResponse = HandleUnauthorizedResponseAsync(httpWebResponse); if (authorizeResponse == null) { return task; // effectively throw } return authorizeResponse .ContinueWith(_ => { _.Wait(); //throw on error return ServerPullAsync(retries + 1); }) .Unwrap(); }).Unwrap(); }) .Unwrap()); }