Ejemplo n.º 1
0
        /// <summary>
        /// The method to send MAPIHTTP request to the server.
        /// </summary>
        /// <param name="site">An instance of interface ITestSite which provides logging, assertions,
        /// and adapters for test code onto its execution context.</param>
        /// <param name="mailStoreUrl">Mail store url.</param>
        /// <param name="userName">The user which connects the server.</param>
        /// <param name="domain">The domain of the user.</param>
        /// <param name="password">The password for the user.</param>
        /// <param name="requestBody">The MAPIHTTP request body.</param>
        /// <param name="requestType">The MAPIHTTP request type.</param>
        /// <param name="cookies">Cookie container for client.</param>
        /// <returns>Return the MAPIHTTP response from the server.</returns>
        public static HttpWebResponse SendMAPIHttpRequest(ITestSite site, string mailStoreUrl, string userName, string domain, string password, IRequestBody requestBody, string requestType, CookieCollection cookies)
        {
            HttpWebResponse response = null;

            System.Net.ServicePointManager.ServerCertificateValidationCallback =
                new System.Net.Security.RemoteCertificateValidationCallback(Common.ValidateServerCertificate);
            System.Net.ServicePointManager.Expect100Continue = false;
            HttpWebRequest request = WebRequest.Create(mailStoreUrl) as HttpWebRequest;

            request.KeepAlive       = true;
            request.CookieContainer = new CookieContainer();
            request.Method          = "POST";
            request.ProtocolVersion = HttpVersion.Version11;
            request.Credentials     = new System.Net.NetworkCredential(userName, password, domain);
            request.ContentType     = "application/mapi-http";
            request.Accept          = "application/mapi-http";
            request.Connection      = string.Empty;

            byte[] buffer = null;
            if (requestBody != null)
            {
                buffer = requestBody.Serialize();
                request.ContentLength = buffer.Length;
            }
            else
            {
                request.ContentLength = 0;
            }

            request.Headers.Add("X-ClientInfo", "{A7A47AAD-233C-412B-9D10-DDE9108FEBD7}-5");
            request.Headers.Add("X-RequestId", "{16AC2587-EED8-48EB-8A7B-D48558B68BD7}:1");
            request.Headers.Add("X-ClientApplication", "Outlook/15.00.0856.000");
            request.Headers.Add("X-RequestType", requestType);
            if (cookies != null && cookies.Count > 0)
            {
                foreach (Cookie cookie in cookies)
                {
                    request.CookieContainer.Add(cookie);
                }
            }

            if (requestBody != null)
            {
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(buffer, 0, buffer.Length);
                }
            }

            try
            {
                response = request.GetResponse() as HttpWebResponse;
            }
            catch (WebException ex)
            {
                site.Assert.Fail("A WebException happened when connecting the server, The exception is {0}.", ex.Message);
            }

            return(response);
        }
        /// <summary>
        /// The method to send MAPIHTTP request to the server.
        /// </summary>
        /// <param name="site">An instance of interface ITestSite which provides logging, assertions,
        /// and adapters for test code onto its execution context.</param>
        /// <param name="mailStoreUrl">Mail store url.</param>
        /// <param name="userName">The user which connects the server.</param>
        /// <param name="domain">The domain of the user.</param>
        /// <param name="password">The password for the user.</param>
        /// <param name="requestBody">The MAPIHTTP request body.</param>
        /// <param name="requestType">The MAPIHTTP request type.</param>
        /// <param name="cookies">Cookie container for client.</param>
        /// <returns>Return the MAPIHTTP response from the server.</returns>
        public static HttpWebResponse SendMAPIHttpRequest(ITestSite site, string mailStoreUrl, string userName, string domain, string password, IRequestBody requestBody, string requestType, CookieCollection cookies)
        {
            HttpWebResponse response = null;

            System.Net.ServicePointManager.ServerCertificateValidationCallback =
            new System.Net.Security.RemoteCertificateValidationCallback(Common.ValidateServerCertificate);
            HttpWebRequest request = WebRequest.Create(mailStoreUrl) as HttpWebRequest;
            request.KeepAlive = true;
            request.CookieContainer = new CookieContainer();
            request.Method = "POST";
            request.ProtocolVersion = HttpVersion.Version11;
            request.Credentials = new System.Net.NetworkCredential(userName, password, domain);
            request.ContentType = "application/mapi-http";
            request.Accept = "application/mapi-http";
            request.Connection = string.Empty;

            byte[] buffer = null;
            if (requestBody != null)
            {
                buffer = requestBody.Serialize();
                request.ContentLength = buffer.Length;
            }
            else
            {
                request.ContentLength = 0;
            }

            request.Headers.Add("X-ClientInfo", "{A7A47AAD-233C-412B-9D10-DDE9108FEBD7}-5");
            request.Headers.Add("X-RequestId", "{16AC2587-EED8-48EB-8A7B-D48558B68BD7}:1");
            request.Headers.Add("X-ClientApplication", "Outlook/15.00.0856.000");
            request.Headers.Add("X-RequestType", requestType);
            if (cookies != null && cookies.Count > 0)
            {
                foreach (Cookie cookie in cookies)
                {
                    request.CookieContainer.Add(cookie);
                }
            }

            if (requestBody != null)
            {
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(buffer, 0, buffer.Length);
                }
            }

            try
            {
                response = request.GetResponse() as HttpWebResponse;
            }
            catch (WebException ex)
            {
                site.Assert.Fail("A WebException happened when connecting the server, The exception is {0}.", ex.Message);
            }

            return response;
        }
        /// <summary>
        /// This method is used to send the http request.
        /// </summary>
        /// <param name="userName">The user name used to connect with server.</param>
        /// <param name="password">The password used to connect with server.</param>
        /// <param name="requestBody">The request body.</param>
        /// <param name="endpoint">The endpoint which the request would be send to.</param>
        /// <param name="cookies">Cookies used to identify the Session Context.</param>
        /// <param name="resquestHeaders">The specified request header used by the request.</param>
        /// <param name="rawBuffer">The raw buffer of the response.</param>
        /// <returns>The response of the request.</returns>
        private HttpWebResponse SendMAPIHttpRequest(string userName, string password, IRequestBody requestBody, ServerEndpoint endpoint, CookieCollection cookies, WebHeaderCollection resquestHeaders, out byte[] rawBuffer)
        {
            rawBuffer = null;
            HttpWebResponse response = null;

            AdapterHelper.Counter++;
            string url = string.Empty;

            if (endpoint == ServerEndpoint.MailboxServerEndpoint)
            {
                if (string.IsNullOrEmpty(this.mailStoreUrl))
                {
                    this.GetEndpointUrl();
                }

                url = this.mailStoreUrl;
            }
            else
            {
                if (string.IsNullOrEmpty(this.addressBookUrl))
                {
                    this.GetEndpointUrl();
                }

                url = this.addressBookUrl;
            }

            System.Net.ServicePointManager.ServerCertificateValidationCallback =
                new System.Net.Security.RemoteCertificateValidationCallback(Common.ValidateServerCertificate);
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

            request.CookieContainer = new CookieContainer();
            request.Method          = "POST";
            request.ProtocolVersion = HttpVersion.Version11;
            request.ContentType     = "application/mapi-http";
            request.Credentials     = new NetworkCredential(userName, password, this.domainName);
            request.Headers.Add(resquestHeaders);
            request.CookieContainer.Add(cookies);
            request.Timeout = System.Threading.Timeout.Infinite;

            byte[] buffer = null;
            if (requestBody != null)
            {
                buffer = requestBody.Serialize();
                request.ContentLength = buffer.Length;
            }
            else
            {
                request.ContentLength = 0;
            }

            if (requestBody != null)
            {
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(buffer, 0, buffer.Length);
                }
            }

            try
            {
                response = request.GetResponse() as HttpWebResponse;

                // Read the HTTP response buffer and parse the response to correct format.
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    rawBuffer = this.ReadHttpResponse(response);
                }
            }
            catch (WebException ex)
            {
                this.Site.Log.Add(
                    LogEntryKind.Comment,
                    "A WebException happened when connecting the server, The exception is {0}.",
                    ex.Message);
                return((HttpWebResponse)ex.Response);
            }

            return(response);
        }
        /// <summary>
        /// This method is used to send the http request.
        /// </summary>
        /// <param name="userName">The user name used to connect with server.</param>
        /// <param name="password">The password used to connect with server.</param>
        /// <param name="requestBody">The request body.</param>
        /// <param name="endpoint">The endpoint which the request would be send to.</param>
        /// <param name="cookies">Cookies used to identify the Session Context.</param>
        /// <param name="resquestHeaders">The specified request header used by the request.</param>
        /// <param name="rawBuffer">The raw buffer of the response.</param>
        /// <returns>The response of the request.</returns>
        private HttpWebResponse SendMAPIHttpRequest(string userName, string password, IRequestBody requestBody, ServerEndpoint endpoint, CookieCollection cookies, WebHeaderCollection resquestHeaders, out byte[] rawBuffer)
        {
            rawBuffer = null;
            HttpWebResponse response = null;
            AdapterHelper.Counter++;
            string url = string.Empty;
            if (endpoint == ServerEndpoint.MailboxServerEndpoint)
            {
                if (string.IsNullOrEmpty(this.mailStoreUrl))
                {
                    this.GetEndpointUrl();
                }

                url = this.mailStoreUrl;
            }
            else
            {
                if (string.IsNullOrEmpty(this.addressBookUrl))
                {
                    this.GetEndpointUrl();
                }

                url = this.addressBookUrl;
            }

            System.Net.ServicePointManager.ServerCertificateValidationCallback =
            new System.Net.Security.RemoteCertificateValidationCallback(Common.ValidateServerCertificate);
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.CookieContainer = new CookieContainer();
            request.Method = "POST";
            request.ProtocolVersion = HttpVersion.Version11;
            request.ContentType = "application/mapi-http";
            request.Credentials = new NetworkCredential(userName, password, this.domainName);
            request.Headers.Add(resquestHeaders);
            request.CookieContainer.Add(cookies);
            request.Timeout = System.Threading.Timeout.Infinite;

            byte[] buffer = null;
            if (requestBody != null)
            {
                buffer = requestBody.Serialize();
                request.ContentLength = buffer.Length;
            }
            else
            {
                request.ContentLength = 0;
            }   

            if (requestBody != null)
            {
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(buffer, 0, buffer.Length);
                }
            }

            try
            {
                response = request.GetResponse() as HttpWebResponse;

                // Read the HTTP response buffer and parse the response to correct format.
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    rawBuffer = this.ReadHttpResponse(response);
                }
            }
            catch (WebException ex)
            {
                this.Site.Log.Add(
                    LogEntryKind.Comment, 
                    "A WebException happened when connecting the server, The exception is {0}.",
                    ex.Message);
                return (HttpWebResponse)ex.Response;
            }

            return response;
        }