Class used to pass the current state of an HTTP connection to IBM.Cloudant.Client.IHttpConnectionInterceptor
This class is used for passing the context for a HTTP connection.
        /// <summary>
        ///  Intercepts the HttpResponse before it is consumed.
        /// </summary>
        /// <returns>The http connection interceptor context</returns>
        /// <param name="context">Http connection interceptor context with the current state</param>
        public HttpConnectionInterceptorContext InterceptResponse(HttpConnectionInterceptorContext context)
        {
            HttpResponseMessage responseMsg = context.responseMsg;

            try
            {
                if ((int)responseMsg.StatusCode == 401)
                {
                    //we need to get a new cookie
                    cookie = GetCookie(responseMsg.RequestMessage.RequestUri);
                    //don't resend request, failed to get cookie
                    if (cookie != null)
                    {
                        context.replayRequest = true;
                    }
                    else
                    {
                        context.replayRequest = false;
                    }
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("Failed to process response interceptor. " + e.Message);
            }

            return(context);
        }
Example #2
0
        /// <summary>
        /// Intercepts the HttpResponse before it is consumed.
        /// </summary>
        /// <returns>The http connection interceptor context</returns>
        /// <param name="context">Http connection interceptor context with the current state.</param>
        public HttpConnectionInterceptorContext InterceptResponse(HttpConnectionInterceptorContext context)
        {
            if (context.responseMsg.StatusCode == System.Net.HttpStatusCode.Unauthorized)
            {
                //Handle authentication error here.

                context.replayRequest = true;
            }
            return(context);
        }
        /// <summary>
        /// Intercepts the HttpResponse before it is consumed.
        /// </summary>
        /// <returns>The http connection interceptor context</returns>
        /// <param name="context">Http connection interceptor context with the current state.</param>
        public HttpConnectionInterceptorContext InterceptResponse(HttpConnectionInterceptorContext context)
        {
            if (context.responseMsg.StatusCode == System.Net.HttpStatusCode.Unauthorized)
            {

                //Handle authentication error here.

                context.replayRequest = true;
            }
            return context;
        }
        /// <summary>
        /// Intercepts the HttpRequest before it is sent.
        /// </summary>
        /// <returns>The http connection interceptor context with any modifications.</returns>
        /// <param name="context">Http connection interceptor context with the current state.</param>
        public HttpConnectionInterceptorContext InterceptRequest(HttpConnectionInterceptorContext context)
        {
            HttpRequestMessage requestMeg = context.requestMsg;

            if (shouldAttemptCookieRequest)
            {
                if (cookie == null)
                {
                    cookie = GetCookie(context.requestMsg.RequestUri);
                }

                context.requestMsg.Headers.Add("Cookie", cookie);
            }

            return context;
        }
        /// <summary>
        /// Intercepts the HttpRequest before it is sent.
        /// </summary>
        /// <returns>The http connection interceptor context with any modifications.</returns>
        /// <param name="context">Http connection interceptor context with the current state.</param>
        public HttpConnectionInterceptorContext InterceptRequest(HttpConnectionInterceptorContext context)
        {
            HttpRequestMessage requestMeg = context.requestMsg;

            if (shouldAttemptCookieRequest)
            {
                if (cookie == null)
                {
                    cookie = GetCookie(context.requestMsg.RequestUri);
                }

                context.requestMsg.Headers.Add("Cookie", cookie);
            }

            return(context);
        }
 /// <summary>
 /// Intercepts the HttpRequest before it is sent.
 /// </summary>
 /// <returns>The http connection interceptor context with any modifications.</returns>
 /// <param name="context">Http connection interceptor context with the current state.</param>
 public HttpConnectionInterceptorContext InterceptRequest(HttpConnectionInterceptorContext context)
 {
     context.requestMsg.Headers.Authorization = authHeader;
     return context;
 }
        /// <summary>
        ///  Intercepts the HttpResponse before it is consumed.
        /// </summary>
        /// <returns>The http connection interceptor context</returns>
        /// <param name="context">Http connection interceptor context with the current state</param>
        public HttpConnectionInterceptorContext InterceptResponse(HttpConnectionInterceptorContext context)
        {
            HttpResponseMessage responseMsg = context.responseMsg;

            try
            {
                if ((int)responseMsg.StatusCode == 401)
                {
                    //we need to get a new cookie
                    cookie = GetCookie(responseMsg.RequestMessage.RequestUri);
                    //don't resend request, failed to get cookie
                    if (cookie != null)
                    {
                        context.replayRequest = true;
                    }
                    else
                    {
                        context.replayRequest = false;
                    }
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("Failed to process response interceptor. " + e.Message);
            }

            return context;
        }
Example #8
0
 /// <summary>
 /// Intercepts the HttpRequest before it is sent.
 /// </summary>
 /// <returns>The http connection interceptor context with any modifications.</returns>
 /// <param name="context">Http connection interceptor context with the current state.</param>
 public HttpConnectionInterceptorContext InterceptRequest(HttpConnectionInterceptorContext context)
 {
     context.requestMsg.Headers.Authorization = authHeader;
     return(context);
 }
Example #9
0
        private async Task<HttpResponseMessage> SendRequestAsync(HttpMethod method,
                                                                 Uri uri,
                                                                 Dictionary<string, string> headers,
                                                                 HttpContent content)
        {
            Debug.WriteLine("HttpHelper:sendRequestAsync()  METHOD:" + method);

            Boolean retry = true;
            int n = numberOfRetries;

            while (retry && n-- > 0)
            {
                retry = false;

                using (var request = new HttpRequestMessage(method, uri))
                {

                    if (headers != null)
                    {
                        foreach (KeyValuePair<string,string> item in headers)
                        {

                            request.Headers.TryAddWithoutValidation(item.Key, item.Value);
                        }
                    }

                    request.Headers.TryAddWithoutValidation("User-Agent", "xamarin-cloudant/0.2");

                    request.Content = content;


                    //Call request interceptors
                    var currHttpConnContext = new HttpConnectionInterceptorContext(
                                                  request,
                                                  null);

                    foreach (IHttpConnectionRequestInterceptor requestInterceptor in requestInterceptors)
                    {
                        currHttpConnContext = requestInterceptor.InterceptRequest(currHttpConnContext);
                    }


                    //Process the request
                    try
                    {
                        Debug.WriteLine(FormatHttpRequestLog(request));

                        HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(continueOnCapturedContext: false);
                        Debug.WriteLine(FormatHttpResponseLog(response));

                        //Call response interceptors
                        currHttpConnContext.responseMsg = response;
                        foreach (IHttpConnectionResponseInterceptor responseInterceptor in responseInterceptors)
                        {
                            currHttpConnContext = responseInterceptor.InterceptResponse(currHttpConnContext);
                        }

                        retry = currHttpConnContext.replayRequest;

                        if (!retry)
                            return response;

                    }
                    catch (AggregateException ae)
                    {
                        Debug.WriteLine("=== Sent request, got EXCEPTION response. Message: " + ae.GetBaseException().Message);
                        ExceptionDispatchInfo.Capture(ae.GetBaseException()).Throw();
                    }
                }
            }

            Debug.WriteLine("Maximum number of retries reached");
            throw new Exception("Maximum number of retries reached.  An HttpConnectionResponseInterceptor has set the replayRequest " +
                "flag to true, but the maximum number of retries [" + numberOfRetries + "] has been reached.");
        }
Example #10
0
        private async Task <HttpResponseMessage> SendRequestAsync(HttpMethod method,
                                                                  Uri uri,
                                                                  Dictionary <string, string> headers,
                                                                  HttpContent content)
        {
            Debug.WriteLine("HttpHelper:sendRequestAsync()  METHOD:" + method);

            Boolean retry = true;
            int     n     = numberOfRetries;

            while (retry && n-- > 0)
            {
                retry = false;

                using (var request = new HttpRequestMessage(method, uri))
                {
                    if (headers != null)
                    {
                        foreach (KeyValuePair <string, string> item in headers)
                        {
                            request.Headers.TryAddWithoutValidation(item.Key, item.Value);
                        }
                    }

                    request.Headers.TryAddWithoutValidation("User-Agent", "xamarin-cloudant/0.2");

                    request.Content = content;


                    //Call request interceptors
                    var currHttpConnContext = new HttpConnectionInterceptorContext(
                        request,
                        null);

                    foreach (IHttpConnectionRequestInterceptor requestInterceptor in requestInterceptors)
                    {
                        currHttpConnContext = requestInterceptor.InterceptRequest(currHttpConnContext);
                    }


                    //Process the request
                    try
                    {
                        Debug.WriteLine(FormatHttpRequestLog(request));

                        HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(continueOnCapturedContext: false);

                        Debug.WriteLine(FormatHttpResponseLog(response));

                        //Call response interceptors
                        currHttpConnContext.responseMsg = response;
                        foreach (IHttpConnectionResponseInterceptor responseInterceptor in responseInterceptors)
                        {
                            currHttpConnContext = responseInterceptor.InterceptResponse(currHttpConnContext);
                        }

                        retry = currHttpConnContext.replayRequest;

                        if (!retry)
                        {
                            return(response);
                        }
                    }
                    catch (AggregateException ae)
                    {
                        Debug.WriteLine("=== Sent request, got EXCEPTION response. Message: " + ae.GetBaseException().Message);
                        ExceptionDispatchInfo.Capture(ae.GetBaseException()).Throw();
                    }
                }
            }

            Debug.WriteLine("Maximum number of retries reached");
            throw new Exception("Maximum number of retries reached.  An HttpConnectionResponseInterceptor has set the replayRequest " +
                                "flag to true, but the maximum number of retries [" + numberOfRetries + "] has been reached.");
        }