/// <summary> /// Generates the failure. /// </summary> /// <param name="request">The request.</param> /// <param name="ex">The ex.</param> /// <returns>The generated exception.</returns> private HttpOutcomeException GenerateFailure(HttpRequestMessage request, Exception ex) { HttpResult result; if (Page == null || Page.Result == null) { var outcome = new HttpOutcome( request.RequestUri, request.Method, HttpStatusCode.BadGateway, string.Empty, TimeSpan.Zero); result = new HttpResult( new[] { outcome }); } else { result = Page.Result; } return(new HttpOutcomeException(result, ex)); }
/// <summary> /// Executes the request internally. /// </summary> /// <typeparam name="T"> /// The type of page to return. /// </typeparam> /// <param name="request"> /// The request. /// </param> /// <param name="expectedStatusCode"> /// The expected status code. /// </param> /// <param name="pageFactory"> /// The page factory. /// </param> /// <returns> /// A <typeparamref name="T" /> value. /// </returns> /// <exception cref="System.ArgumentNullException"> /// The <paramref name="request" /> parameter is <c>null</c>. /// </exception> /// <exception cref="System.ArgumentNullException"> /// The <paramref name="pageFactory" /> parameter is <c>null</c>. /// </exception> /// <exception cref="HttpOutcomeException"> /// </exception> private T ExecuteInternal <T>( HttpRequestMessage request, HttpStatusCode expectedStatusCode, IPageFactory pageFactory) where T : IPage, new() { var page = default(T); if (request == null) { throw new ArgumentNullException("request"); } if (pageFactory == null) { throw new ArgumentNullException("pageFactory"); } request.Headers.Add("user-agent", UserAgent); var currentResourceLocation = request.RequestUri; var outcomes = new List <HttpOutcome>(); var stopwatch = Stopwatch.StartNew(); var task = _client.SendAsync(request); Uri redirectLocation = null; bool requiresRedirect; using (var response = task.Result) { stopwatch.Stop(); var outcome = new HttpOutcome( currentResourceLocation, response.RequestMessage.Method, response.StatusCode, response.ReasonPhrase, stopwatch.Elapsed); outcomes.Add(outcome); requiresRedirect = IsRedirect(response); if (requiresRedirect) { redirectLocation = response.Headers.Location; } else { // This the final response var result = new HttpResult(outcomes); page = pageFactory.Create <T>(this, response, result); } } while (requiresRedirect) { // Get the redirect address Uri fullRedirectLocation; if (redirectLocation.IsAbsoluteUri) { fullRedirectLocation = redirectLocation; } else { fullRedirectLocation = new Uri(currentResourceLocation, redirectLocation); } currentResourceLocation = fullRedirectLocation; stopwatch = Stopwatch.StartNew(); task = _client.GetAsync(currentResourceLocation); using (var response = task.Result) { stopwatch.Stop(); var outcome = new HttpOutcome( currentResourceLocation, response.RequestMessage.Method, response.StatusCode, response.ReasonPhrase, stopwatch.Elapsed); outcomes.Add(outcome); requiresRedirect = IsRedirect(response); if (requiresRedirect) { redirectLocation = response.Headers.Location; } else { // This the final response var result = new HttpResult(outcomes); page = pageFactory.Create <T>(this, response, result); } } } SetCurrentPage(page); var lastOutcome = outcomes[outcomes.Count - 1]; if (lastOutcome.StatusCode != expectedStatusCode) { var result = new HttpResult(outcomes); throw new HttpOutcomeException(result, expectedStatusCode); } // Validate that the final address matches the page if (page.IsOn(currentResourceLocation) == false) { var result = new HttpResult(outcomes); throw new HttpOutcomeException(result, page.TargetLocation); } return(page); }