Esempio n. 1
0
        protected virtual HttpResponseMessage CreateFormPostResponse(IDirectedProtocolMessage message, IDictionary <string, string> fields)
        {
            using (StringWriter bodyWriter = new StringWriter(CultureInfo.InvariantCulture))
            {
                StringBuilder hiddenFields = new StringBuilder();
                foreach (var field in fields)
                {
                    hiddenFields.AppendFormat("\t<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />\r\n", HttpUtility.HtmlEncode(field.Key), HttpUtility.HtmlEncode(field.Value));
                }
                bodyWriter.WriteLine(
                    IndirectMessageFormPostFormat,
                    HttpUtility.HtmlEncode(message.Recipient.AbsoluteUri),
                    hiddenFields
                    );
                bodyWriter.Flush();
                HttpResponseMessage response = new HttpResponseMessageWithOriginal(message)
                {
                    StatusCode = HttpStatusCode.OK,
                    Content    = new StringContent(bodyWriter.ToString())
                };
                response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("text/html");

                return(response);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 重定向
        /// </summary>
        /// <param name="message"></param>
        /// <param name="fields"></param>
        /// <param name="payloadInFragment"></param>
        /// <returns></returns>
        protected virtual HttpResponseMessage Create301RedirectResponse(IDirectedProtocolMessage message, IDictionary <string, string> fields, bool payloadInFragment = false)
        {
            UriBuilder builder = new UriBuilder(message.Recipient);

            if (payloadInFragment)
            {
                builder.AppendFragmentArgs(fields);
            }
            else
            {
                builder.AppendQueryArgs(fields);
            }
            Logger.Http.DebugFormat("Redirecting to {0}", builder.Uri.AbsoluteUri);
            HttpResponseMessage response = new HttpResponseMessageWithOriginal(message)
            {
                StatusCode = HttpStatusCode.Redirect,
                Content    = new StringContent(string.Format(CultureInfo.InvariantCulture, RedirectResponseBodyFormat, builder.Uri.AbsoluteUri))
            };

            response.Headers.Location            = builder.Uri;
            response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("text/html")
            {
                CharSet = "utf-8"
            };
            return(response);
        }