Beispiel #1
0
 public void Dispose()
 {
     if (WebResponse != null)
     {
         WebResponse.Close();
     }
     if (ConnectionId != null)
     {
         ConnectionStatistics.DiscardConnection(ConnectionId.Value);
     }
 }
Beispiel #2
0
        protected Response HandleResponse(HttpWebResponse response, string method, bool includeBody, Action <HttpWebRequest> requestModifier, int retryLevel = 0)
        {
            Guid?connectionId = null;

            try
            {
                connectionId = ConnectionStatistics.RegisterConnection(response);

                ApplyResponseInterceptors(response);

                // Handle redirects
                if (300 <= (int)response.StatusCode && (int)response.StatusCode <= 399)
                {
                    int allowedRedirectCount = Session.GetAllowedRedirects((int)response.StatusCode);
                    if (retryLevel < allowedRedirectCount)
                    {
                        bool allowAutomaticRedirect = false;
                        if (response.StatusCode == HttpStatusCode.SeeOther)
                        {
                            method                 = "GET";
                            includeBody            = false;
                            allowAutomaticRedirect = true;
                        }
                        else if (method.Equals("GET", StringComparison.InvariantCultureIgnoreCase) ||
                                 method.Equals("HEAD", StringComparison.InvariantCultureIgnoreCase))
                        {
                            allowAutomaticRedirect = true;
                        }

                        if (allowAutomaticRedirect)
                        {
                            Uri location = response.LocationAsUri();
                            if (location == null)
                            {
                                throw new InvalidOperationException(string.Format("No redirect location supplied in {0} response from {1}.", (int)response.StatusCode, response.ResponseUri));
                            }

                            response.Close();
                            if (connectionId != null)
                            {
                                ConnectionStatistics.DiscardConnection(connectionId.Value);
                            }

                            response.Dispose();

                            return(DoRequest(location, method, includeBody, requestModifier, retryLevel + 1));
                        }
                    }
                }

                return(new Response(response, Session, retryLevel, connectionId));
            }
            catch (Exception)
            {
                response.Close();
                if (connectionId != null)
                {
                    ConnectionStatistics.DiscardConnection(connectionId.Value);
                }
                throw;
            }
        }