예제 #1
0
        public async Task <IObservable <string> > ServerPullAsync()
        {
            int retries = 0;

            while (true)
            {
                WebException    webException;
                HttpWebResponse httpWebResponse;

                try
                {
                    var response = await webRequest.GetResponseAsync();

                    var stream = response.GetResponseStreamWithHttpDecompression();
                    var observableLineStream = new ObservableLineStream(stream, () =>
                    {
                        webRequest.Abort();
                        response.Close();
                    });
                    observableLineStream.Start();
                    return((IObservable <string>)observableLineStream);
                }
                catch (WebException e)
                {
                    if (++retries >= 3 || disabledAuthRetries)
                    {
                        throw;
                    }

                    httpWebResponse = e.Response as HttpWebResponse;
                    if (httpWebResponse == null ||
                        (httpWebResponse.StatusCode != HttpStatusCode.Unauthorized &&
                         httpWebResponse.StatusCode != HttpStatusCode.Forbidden &&
                         httpWebResponse.StatusCode != HttpStatusCode.PreconditionFailed))
                    {
                        throw;
                    }

                    webException = e;
                }

                if (httpWebResponse.StatusCode == HttpStatusCode.Forbidden)
                {
                    await HandleForbiddenResponseAsync(httpWebResponse);

                    await new CompletedTask(webException).Task;                     // Throws, preserving original stack
                }

                if (await HandleUnauthorizedResponseAsync(httpWebResponse) == false)
                {
                    await new CompletedTask(webException).Task;                     // Throws, preserving original stack
                }
            }
        }
예제 #2
0
        public Task <IObservable <string> > ServerPullAsync(int retries = 0)
        {
            return(Task.Factory.FromAsync <WebResponse>(webRequest.BeginGetResponse, webRequest.EndGetResponse, null)
                   .ContinueWith(task =>
            {
                var stream = task.Result.GetResponseStreamWithHttpDecompression();
                var observableLineStream = new ObservableLineStream(stream, () =>
                {
                    webRequest.Abort();
                    task.Result.Close();
                });
                observableLineStream.Start();
                return (IObservable <string>)observableLineStream;
            })
                   .ContinueWith(task =>
            {
                var webException = task.Exception.ExtractSingleInnerException() as WebException;
                if (webException == null || retries >= 3)
                {
                    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());
        }
예제 #3
0
        public async Task <IObservable <string> > ServerPullAsync()
        {
            return(await RunWithAuthRetry(async() =>
            {
                var httpRequestMessage = new HttpRequestMessage(new HttpMethod(Method), Url);
                this.Response = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead);
                SetResponseHeaders(Response);

                await CheckForErrorsAndReturnCachedResultIfAnyAsync(readErrorString: true).ConfigureAwait(false);

                var stream = await Response.Content.ReadAsStreamAsync();
                var observableLineStream = new ObservableLineStream(stream, () => Response.Dispose());
                observableLineStream.Start();
                return (IObservable <string>)observableLineStream;
            }).ConfigureAwait(false));
        }