public static Task <bool> ReadAsync([NotNull] this WebRequest thisValue, [NotNull] IOResponseSettings settings, CancellationToken token = default(CancellationToken)) { token.ThrowIfCancellationRequested(); return(GetResponseAsync(thisValue, settings, token) .ContinueWith(task => { WebResponse response = task.Result; if (response == null) { return false; } try { if (token.IsCancellationRequested) { return false; } return settings.OnResponseReceived?.Invoke(response) != false && response.ReadAsync(settings, token).Execute(); } catch (Exception ex) when(settings.OnError != null) { settings.OnError(ex); return false; } finally { ObjectHelper.Dispose(ref response); thisValue.Abort(); } }, token, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Default)); }
public static Task <Stream> GetStreamAsync([NotNull] this WebRequest thisValue, IOResponseSettings settings = null, CancellationToken token = default(CancellationToken)) { token.ThrowIfCancellationRequested(); return(GetResponseAsync(thisValue, settings, token) .ContinueWith(task => { WebResponse response = task.Result; if (response == null) { return null; } try { if (token.IsCancellationRequested) { return null; } return settings?.OnResponseReceived?.Invoke(response) == false ? null : response.GetStream(); } catch (Exception ex) when(settings?.OnError != null) { settings.OnError(ex); return null; } }, token, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Default)); }
public static Task <(string Title, string Buffer)> PeekAsync([NotNull] this WebRequest thisValue, IOResponseSettings settings = null, CancellationToken token = default(CancellationToken)) { token.ThrowIfCancellationRequested(); return(GetResponseAsync(thisValue, settings, token) .ContinueWith(task => { WebResponse response = task.Result; if (response == null) { return (null, null); } try { return token.IsCancellationRequested ? (null, null) : settings?.OnResponseReceived?.Invoke(response) == false ? (null, null) : response.PeekAsync(settings, token).Execute(); } catch (Exception ex) when(settings?.OnError != null) { settings.OnError(ex); return (null, null); } finally { ObjectHelper.Dispose(ref response); thisValue.Abort(); } }, token, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Default)); }
public static (string Title, string Buffer) Peek([NotNull] this WebRequest thisValue, IOResponseSettings settings = null) { WebResponse response = GetResponse(thisValue, settings); if (response == null) { return(null, null); } try { return(settings?.OnResponseReceived?.Invoke(response) == false ? (null, null) : response.Peek(settings)); } catch (Exception ex) when(settings?.OnError != null) { settings.OnError(ex); return(null, null); } finally { ObjectHelper.Dispose(ref response); thisValue.Abort(); } }
public static Stream GetStream([NotNull] this WebRequest thisValue, IOResponseSettings settings = null) { WebResponse response = GetResponse(thisValue, settings); if (response == null) { return(null); } try { return(settings?.OnResponseReceived?.Invoke(response) == false ? null : response.GetStream()); } catch (Exception ex) when(settings?.OnError != null) { settings.OnError(ex); return(null); } }
public static bool Read([NotNull] this WebRequest thisValue, [NotNull] IOResponseSettings settings) { WebResponse response = GetResponse(thisValue, settings); if (response == null) { return(false); } try { return(settings.OnResponseReceived?.Invoke(response) != false && response.Read(settings)); } catch (Exception ex) when(settings.OnError != null) { settings.OnError(ex); return(false); } finally { ObjectHelper.Dispose(ref response); thisValue.Abort(); } }