Exemplo n.º 1
0
        /// <summary>
        /// Prepares to send a request to the Service Provider as the payload of a POST request.
        /// </summary>
        /// <param name="requestMessage">The message to be transmitted to the ServiceProvider.</param>
        /// <returns>The web request ready to send.</returns>
        /// <remarks>
        /// This method is simply a standard HTTP POST request with the message parts serialized to the POST entity
        /// with the application/x-www-form-urlencoded content type
        /// This method satisfies OAuth 1.0 section 5.2, item #2 and OpenID 2.0 section 4.1.2.
        /// </remarks>
        protected virtual HttpWebRequest InitializeRequestAsPost(IDirectedProtocolMessage requestMessage)
        {
            ErrorUtilities.VerifyArgumentNotNull(requestMessage, "requestMessage");

            var serializer = MessageSerializer.Get(requestMessage.GetType());
            var fields     = serializer.Serialize(requestMessage);

            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(requestMessage.Recipient);

            httpRequest.CachePolicy = this.CachePolicy;
            httpRequest.Method      = "POST";
            httpRequest.ContentType = "application/x-www-form-urlencoded";
            httpRequest.Headers[HttpRequestHeader.ContentEncoding] = PostEntityEncoding.WebName;
            string requestBody = MessagingUtilities.CreateQueryString(fields);

            byte[] requestBytes = PostEntityEncoding.GetBytes(requestBody);
            httpRequest.ContentLength = requestBytes.Length;
            Stream requestStream = this.WebRequestHandler.GetRequestStream(httpRequest);

            try {
                requestStream.Write(requestBytes, 0, requestBytes.Length);
            } finally {
                // We need to be sure to close the request stream...
                // unless it is a MemoryStream, which is a clue that we're in
                // a mock stream situation and closing it would preclude reading it later.
                if (!(requestStream is MemoryStream))
                {
                    requestStream.Dispose();
                }
            }

            return(httpRequest);
        }
        /// <summary>
        /// Serializes the <see cref="DataBag"/> instance to a buffer.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns>The buffer containing the serialized data.</returns>
        protected override byte[] SerializeCore(T message)
        {
            var    fields = MessageSerializer.Get(message.GetType()).Serialize(MessageDescriptions.GetAccessor(message));
            string value  = MessagingUtilities.CreateQueryString(fields);

            return(Encoding.UTF8.GetBytes(value));
        }