Example #1
0
        /// <summary>
        /// Builds the IEwsHttpWebRequest object for current service request with exception handling.
        /// </summary>
        /// <returns>An IEwsHttpWebRequest instance</returns>
        protected async Task <IEwsHttpWebRequest> BuildEwsHttpWebRequest()
        {
            IEwsHttpWebRequest request = null;

            try
            {
                request = this.Service.PrepareHttpWebRequest(this.GetXmlElementName());

                this.Service.TraceHttpRequestHeaders(TraceFlags.EwsRequestHttpHeaders, request);

                bool needSignature = this.Service.Credentials != null && this.Service.Credentials.NeedSignature;
                bool needTrace     = this.Service.IsTraceEnabledFor(TraceFlags.EwsRequest);

                // The request might need to add additional headers
                this.AddHeaders(request.Headers);

                // If tracing is enabled, we generate the request in-memory so that we
                // can pass it along to the ITraceListener. Then we copy the stream to
                // the request stream.
                if (needSignature || needTrace)
                {
                    this.TraceAndEmitRequest(request, needSignature, needTrace);
                }
                else
                {
                    this.EmitRequest(request);
                }

                return(request);
            }
            catch (EwsHttpClientException ex)
            {
                if (ex.IsProtocolError && ex.Response != null)
                {
                    await this.ProcessEwsHttpClientException(ex);
                }
                if (request != null)
                {
                    request.Dispose();
                }

                // Wrap exception if the above code block didn't throw
                throw new ServiceRequestException(string.Format(Strings.ServiceRequestFailed, ex.Message), ex);
            }
            catch (IOException e)
            {
                if (request != null)
                {
                    request.Dispose();
                }
                // Wrap exception.
                throw new ServiceRequestException(string.Format(Strings.ServiceRequestFailed, e.Message), e);
            }
        }
        /// <summary>
        /// Creates an HttpWebRequest instance and initializes it with the appropriate parameters,
        /// based on the configuration of this service object.
        /// </summary>
        /// <param name="url">The URL that the HttpWebRequest should target.</param>
        /// <param name="acceptGzipEncoding">If true, ask server for GZip compressed content.</param>
        /// <param name="allowAutoRedirect">If true, redirection responses will be automatically followed.</param>
        /// <returns>A initialized instance of HttpWebRequest.</returns>
        internal IEwsHttpWebRequest PrepareHttpWebRequestForUrl(
            Uri url,
            bool acceptGzipEncoding,
            bool allowAutoRedirect)
        {
            // Verify that the protocol is something that we can handle
            if ((url.Scheme != "http") && (url.Scheme != "https"))
            {
                throw new ServiceLocalException(string.Format(Strings.UnsupportedWebProtocol, url.Scheme));
            }

            IEwsHttpWebRequest request = this.HttpWebRequestFactory.CreateRequest(url);

            try
            {
                request.PreAuthenticate = this.PreAuthenticate;
                request.Timeout         = this.Timeout;
                this.SetContentType(request);
                request.Method              = "POST";
                request.UserAgent           = this.UserAgent;
                request.AllowAutoRedirect   = allowAutoRedirect;
                request.CookieContainer     = this.CookieContainer;
                request.KeepAlive           = this.keepAlive;
                request.ConnectionGroupName = this.connectionGroupName;

                if (acceptGzipEncoding)
                {
                    request.Headers.AcceptEncoding.ParseAdd("gzip,deflate");
                }

                if (!string.IsNullOrEmpty(this.clientRequestId))
                {
                    request.Headers.TryAddWithoutValidation("client-request-id", this.clientRequestId);
                    if (this.returnClientRequestId)
                    {
                        request.Headers.TryAddWithoutValidation("return-client-request-id", "true");
                    }
                }

                if (this.webProxy != null)
                {
                    request.Proxy = this.webProxy;
                }

                if (this.HttpHeaders.Count > 0)
                {
                    this.HttpHeaders.ForEach((kv) => request.Headers.TryAddWithoutValidation(kv.Key, kv.Value));
                }

                request.UseDefaultCredentials = this.UseDefaultCredentials;
                if (!request.UseDefaultCredentials)
                {
                    ExchangeCredentials serviceCredentials = this.Credentials;
                    if (serviceCredentials == null)
                    {
                        throw new ServiceLocalException(Strings.CredentialsRequired);
                    }
                    // Make sure that credentials have been authenticated if required
                    serviceCredentials.PreAuthenticate();

                    // Apply credentials to the request
                    serviceCredentials.PrepareWebRequest(request);
                }

                lock (this.httpResponseHeaders)
                {
                    this.httpResponseHeaders.Clear();
                }

                return(request);
            }
            catch (Exception)
            {
                request.Dispose();
                throw;
            }
        }