/// <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 != Uri.UriSchemeHttp) && (url.Scheme != Uri.UriSchemeHttps))
            {
                throw new ServiceLocalException(string.Format(Strings.UnsupportedWebProtocol, url.Scheme));
            }

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

            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.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
            }

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

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

            if (this.HttpHeaders.Count > 0)
            {
                this.HttpHeaders.ForEach((kv) => request.Headers.Add(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);
            }

            this.httpResponseHeaders.Clear();

            return(request);
        }
Ejemplo n.º 2
0
        /// <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);

            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[HttpRequestHeader.AcceptEncoding] = "gzip,deflate";
            }

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

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

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

            request.UseDefaultCredentials = this.UseDefaultCredentials;
            if (!request.UseDefaultCredentials)
            {
                ExchangeCredentials serviceCredentials = this.Credentials;
                if (serviceCredentials == null)
                {
                    throw new ServiceLocalException(Strings.CredentialsRequired);
                }

#if NETSTANDARD2_0
                // Temporary fix for authentication on Linux platform
                if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    serviceCredentials = AdjustLinuxAuthentication(url, serviceCredentials);
                }
#endif

                // 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);
        }