InitializeHTTPHeader() public static method

Initialize HTTP Header
public static InitializeHTTPHeader ( RequestType requestType, string clientInstance, int counter ) : WebHeaderCollection
requestType RequestType The request type
clientInstance string The string of the client instance
counter int The counter
return System.Net.WebHeaderCollection
        /// <summary>
        /// Send the request to address book server endpoint.
        /// </summary>
        /// <param name="requestBody">The request body.</param>
        /// <param name="requestType">The type of the request.</param>
        /// <param name="cookieChange">If the session context cookie changed.</param>
        /// <returns>The common response.</returns>
        private CommonResponse SendAddressBookRequest(IRequestBody requestBody, RequestType requestType, bool cookieChange = true)
        {
            byte[]              rawBuffer;
            CommonResponse      commonResponse      = null;
            WebHeaderCollection webHeaderCollection = AdapterHelper.InitializeHTTPHeader(requestType, AdapterHelper.ClientInstance, AdapterHelper.Counter);

            // Send the Execute HTTP request and get the response.
            HttpWebResponse response     = this.SendMAPIHttpRequest(this.userName, this.password, requestBody, ServerEndpoint.AddressBookServerEndpoint, AdapterHelper.SessionContextCookies, webHeaderCollection, out rawBuffer);
            uint            responseCode = AdapterHelper.GetFinalResponseCode(response.Headers["X-ResponseCode"]);

            this.Site.Assert.AreEqual <uint>(0, responseCode, "The request to the address book server should be executed successfully!");

            // Read the HTTP response buffer and parse the response to correct format.
            commonResponse = CommonResponse.ParseCommonResponse(rawBuffer);
            Site.Assert.IsNotNull(commonResponse.ResponseBodyRawData, "The response body should contains data.");
            this.VerifyRequestTypesForAddressBookServerEndpoint(response.Headers, commonResponse);
            this.VerifyAutoDiscover(response.StatusCode, ServerEndpoint.AddressBookServerEndpoint);
            this.VerifyAuthentication(response);

            response.GetResponseStream().Close();
            if (cookieChange)
            {
                AdapterHelper.SessionContextCookies = response.Cookies;
            }

            return(commonResponse);
        }
        /// <summary>
        /// This method is used by the client to get specific properties on an object.
        /// </summary>
        /// <param name="getPropsRequestBody">The GetProps request type request body.</param>
        /// <param name="responseCodeHeader">The value of X-ResponseCode header</param>
        /// <returns>The response body of the GetProps request type.</returns>
        public GetPropsResponseBody GetProps(GetPropsRequestBody getPropsRequestBody, out uint responseCodeHeader)
        {
            byte[]              rawBuffer;
            CommonResponse      commonResponse      = null;
            WebHeaderCollection webHeaderCollection = AdapterHelper.InitializeHTTPHeader(RequestType.GetProps, AdapterHelper.ClientInstance, AdapterHelper.Counter);

            // Send the Execute HTTP request and get the response.
            HttpWebResponse response = this.SendMAPIHttpRequest(this.userName, this.password, getPropsRequestBody, ServerEndpoint.AddressBookServerEndpoint, AdapterHelper.SessionContextCookies, webHeaderCollection, out rawBuffer);

            responseCodeHeader = AdapterHelper.GetFinalResponseCode(response.Headers["X-ResponseCode"]);

            // Read the HTTP response buffer and parse the response to correct format.
            commonResponse = CommonResponse.ParseCommonResponse(rawBuffer);

            response.GetResponseStream().Close();
            AdapterHelper.SessionContextCookies = response.Cookies;
            GetPropsResponseBody getPropsResponseBody = new GetPropsResponseBody();

            if (commonResponse.ResponseBodyRawData.Length > 0)
            {
                getPropsResponseBody = GetPropsResponseBody.Parse(commonResponse.ResponseBodyRawData);
            }

            return(getPropsResponseBody);
        }
        /// <summary>
        /// This method allows a client to determine whether a server's endpoint is reachable and operational.
        /// </summary>
        /// <param name="endpoint">The endpoint used by PING request.</param>
        /// <param name="metatags">The meta tags in the response body of the Ping request type.</param>
        /// <param name="headers">The request and response header of the PING request type.</param>
        /// <returns>The status code of the PING request type.</returns>
        public uint PING(ServerEndpoint endpoint, out List <string> metatags, out WebHeaderCollection headers)
        {
            metatags = null;
            byte[] rawBuffer;
            WebHeaderCollection webHeaderCollection = AdapterHelper.InitializeHTTPHeader(RequestType.PING, AdapterHelper.ClientInstance, AdapterHelper.Counter);

            // Send the PING HTTP request and get the response.
            HttpWebResponse response = this.SendMAPIHttpRequest(this.userName, this.password, null, endpoint, AdapterHelper.SessionContextCookies, webHeaderCollection, out rawBuffer);

            headers = response.Headers;

            // Read the HTTP response buffer and parse the response to correct format.
            uint responseCode = AdapterHelper.GetFinalResponseCode(response.Headers["X-ResponseCode"]);

            CommonResponse commonResponse = CommonResponse.ParseCommonResponse(rawBuffer);

            if (responseCode == 0)
            {
                // PING succeeded when the response code equals zero and PING request has no response body.
                metatags = commonResponse.MetaTags;
                this.VerifyHTTPS(response);
                this.VerifyAuthentication(response);
                this.VerifyAutoDiscover(response.StatusCode, endpoint);
                this.VerifyHTTPHeaders(response.Headers);
                this.VerifyAdditionalHeaders(commonResponse.AdditionalHeaders);
                this.VerifyPINGRequestType(commonResponse, endpoint, responseCode);
                this.VerifyResponseMetaTags(commonResponse.MetaTags);
            }

            this.VerifyContentTypeHeader(response.Headers);
            this.VerifyRespondingToAllRequestTypeRequests(response, commonResponse, responseCode);
            response.GetResponseStream().Close();
            return(responseCode);
        }
        /// <summary>
        /// This method is used by the client to establish a Session Context with the Address Book Server.
        /// </summary>
        /// <param name="bindRequestBody">The bind request type request body.</param>
        /// <param name="responseCode">The value of X-ResponseCode header of the bind response.</param>
        /// <returns>The response body of bind request type.</returns>
        public BindResponseBody Bind(BindRequestBody bindRequestBody, out int responseCode)
        {
            byte[]           rawBuffer        = null;
            CommonResponse   commonResponse   = null;
            BindResponseBody bindResponseBody = null;

            AdapterHelper.Counter        = 1;
            AdapterHelper.ClientInstance = Guid.NewGuid().ToString();
            WebHeaderCollection webHeaderCollection = AdapterHelper.InitializeHTTPHeader(RequestType.Bind, AdapterHelper.ClientInstance, AdapterHelper.Counter);

            // Send the Execute HTTP request and get the response.
            HttpWebResponse response = this.SendMAPIHttpRequest(this.userName, this.password, bindRequestBody, ServerEndpoint.AddressBookServerEndpoint, AdapterHelper.SessionContextCookies, webHeaderCollection, out rawBuffer);

            responseCode = (int)AdapterHelper.GetFinalResponseCode(response.Headers["X-ResponseCode"]);

            // Read the HTTP response buffer and parse the response to correct format.
            if (responseCode == 0)
            {
                commonResponse = CommonResponse.ParseCommonResponse(rawBuffer);
                Site.Assert.IsNotNull(commonResponse.ResponseBodyRawData, "The response body should contains data.");
                bindResponseBody = BindResponseBody.Parse(commonResponse.ResponseBodyRawData);
                this.VerifyBindResponseBody(bindResponseBody);
                this.VerifyAutoDiscover(response.StatusCode, ServerEndpoint.AddressBookServerEndpoint);
            }

            this.VerifyAuthentication(response);
            response.GetResponseStream().Close();
            AdapterHelper.SessionContextCookies = response.Cookies;

            return(bindResponseBody);
        }
        /// <summary>
        /// This method is used by the client to delete a Session Context with the server.
        /// </summary>
        /// <param name="responseBody">The response body of the Disconnect request type.</param>
        /// <returns>The status code of the Disconnect request type.</returns>
        public uint Disconnect(out MailboxResponseBodyBase responseBody)
        {
            responseBody = null;
            byte[] rawBuffer;

            // Prepare the disconnect request body.
            DisconnectRequestBody disconnectRequestBody = new DisconnectRequestBody();

            byte[] rgbAuxIn = new byte[] { };
            disconnectRequestBody.AuxiliaryBufferSize = (uint)rgbAuxIn.Length;
            disconnectRequestBody.AuxiliaryBuffer     = rgbAuxIn;
            WebHeaderCollection webHeaderCollection = AdapterHelper.InitializeHTTPHeader(RequestType.Disconnect, AdapterHelper.ClientInstance, AdapterHelper.Counter);

            // Send the disconnect HTTP request and get the response.
            HttpWebResponse response     = this.SendMAPIHttpRequest(this.userName, this.password, disconnectRequestBody, ServerEndpoint.MailboxServerEndpoint, AdapterHelper.SessionContextCookies, webHeaderCollection, out rawBuffer);
            uint            responseCode = AdapterHelper.GetFinalResponseCode(response.Headers["X-ResponseCode"]);

            // Read the HTTP response buffer and parse the response to correct format.
            CommonResponse commonResponse = CommonResponse.ParseCommonResponse(rawBuffer);

            if (responseCode == 0)
            {
                Site.Assert.IsNotNull(commonResponse.ResponseBodyRawData, "The response body should contains data.");
                uint statusCode = BitConverter.ToUInt32(commonResponse.ResponseBodyRawData, 0);
                if (statusCode == 0)
                {
                    // Disconnect succeeded when the StatusCode field equals zero.
                    DisconnectSuccessResponseBody responseSuccess = DisconnectSuccessResponseBody.Parse(commonResponse.ResponseBodyRawData);
                    responseBody = responseSuccess;

                    this.VerifyDisconnectSuccessResponseBody(responseSuccess);
                }

                this.VerifyHTTPS(response);
                this.VerifyAuthentication(response);
                this.VerifyAutoDiscover(response.StatusCode, ServerEndpoint.MailboxServerEndpoint);
                this.VerifyHTTPHeaders(response.Headers);
                this.VerifyAdditionalHeaders(commonResponse.AdditionalHeaders);
                this.VerifyDisconnectResponse(response);
                this.VerifyRequestTypesForMailboxServerEndpoint(response.Headers, commonResponse);
                this.VerifyResponseMetaTags(commonResponse.MetaTags);
            }

            this.VerifyContentTypeHeader(response.Headers);
            this.VerifyRespondingToAllRequestTypeRequests(response, commonResponse, responseCode);
            response.GetResponseStream().Close();
            AdapterHelper.SessionContextCookies = response.Cookies;
            return(responseCode);
        }