Exemple #1
0
        /// <summary>
        /// Get the HTTP response for this request.
        /// </summary>
        /// <returns>The HTTP response.</returns>
        private async Task <IServiceFilterResponse> GetResponseAsync()
        {
            HttpWebResponse             response    = null;
            ServiceFilterResponseStatus errorStatus = ServiceFilterResponseStatus.Success;

            try
            {
                // Create the request and send for the response
                HttpWebRequest request = await this.CreateHttpWebRequestAsync();

                response = await
                           Task <WebResponse> .Factory.FromAsync(
                    request.BeginGetResponse,
                    request.EndGetResponse,
                    request) as HttpWebResponse;
            }
            catch (WebException ex)
            {
                // Don't raise WebExceptions for >= 400 responses because it's
                // actually up to the various filters whether or not we'll
                // return the failure to the user or whether we'll make another
                // request.  The MobileServiceClient.RequestAsync method will
                // check the final response it receives for errors and throw an
                // exception if appropriate.
                response    = ex.Response as HttpWebResponse;
                errorStatus = (ServiceFilterResponseStatus)ex.Status;
            }

            return(new ServiceFilterResponse(response, errorStatus));
        }
        /// <summary>
        /// Initializes a new instance of the ServiceFilterResponse class.
        /// </summary>
        /// <param name="response">
        /// The HttpWebResponse backing this request.
        /// </param>
        /// <param name="errorStatus">
        /// The error status for any connection failures.
        /// </param>
        public ServiceFilterResponse(HttpWebResponse response, ServiceFilterResponseStatus errorStatus)
        {
            this.ResponseStatus = errorStatus;
            this.Headers        = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            // Read all the interesting values from the response if it's
            // provided
            if (response != null)
            {
                // Pull out the headers
                foreach (string key in response.Headers.AllKeys)
                {
                    this.Headers[key] = response.Headers[key];
                }

                // Get the status code
                this.StatusCode = (int)response.StatusCode;

                // Get the status description
                this.StatusDescription = response.StatusDescription;

                // Get the content
                this.ContentType = response.ContentType;
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    this.Content = reader.ReadToEnd();
                }
            }
            else
            {
                // Provide sane defaults that will likely prevent folks from
                // crashing if they're not careful to check ErrorStatus first
                this.StatusCode        = 0;
                this.StatusDescription = string.Empty;
                this.ContentType       = string.Empty;
                this.Content           = string.Empty;
            }
        }
        /// <summary>
        /// Initializes a new instance of the ServiceFilterResponse class.
        /// </summary>
        /// <param name="response">
        /// The HttpWebResponse backing this request.
        /// </param>
        /// <param name="errorStatus">
        /// The error status for any connection failures.
        /// </param>
        public ServiceFilterResponse(HttpWebResponse response, ServiceFilterResponseStatus errorStatus)
        {
            this.ResponseStatus = errorStatus;
            this.Headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

            // Read all the interesting values from the response if it's
            // provided
            if (response != null)
            {
                // Pull out the headers
                foreach (string key in response.Headers.AllKeys)
                {
                    this.Headers[key] = response.Headers[key];
                }

                // Get the status code
                this.StatusCode = (int)response.StatusCode;

                // Get the status description
                this.StatusDescription = response.StatusDescription;

                // Get the content
                this.ContentType = response.ContentType;
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    this.Content = reader.ReadToEnd();
                }
            }
            else
            {
                // Provide sane defaults that will likely prevent folks from
                // crashing if they're not careful to check ErrorStatus first
                this.StatusCode = 0;
                this.StatusDescription = string.Empty;
                this.ContentType = string.Empty;
                this.Content = string.Empty; 
            }
        }