当前位置: 首页>>代码示例>>C#>>正文


C# HttpResponseMessage.AddHeader方法代码示例

本文整理汇总了C#中System.Net.Http.HttpResponseMessage.AddHeader方法的典型用法代码示例。如果您正苦于以下问题:C# HttpResponseMessage.AddHeader方法的具体用法?C# HttpResponseMessage.AddHeader怎么用?C# HttpResponseMessage.AddHeader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.Http.HttpResponseMessage的用法示例。


在下文中一共展示了HttpResponseMessage.AddHeader方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ConfigureHttpResponseMessage

        public void ConfigureHttpResponseMessage(Message message, HttpResponseMessage httpResponseMessage, HttpResponseMessageProperty responseProperty)
        {
            HttpChannelUtilities.EnsureHttpResponseMessageContentNotNull(httpResponseMessage);

            string action = message.Headers.Action;

            if (message.Version.Addressing == AddressingVersion.None)
            {
                if (MessageLogger.LogMessagesAtTransportLevel)
                {
                    message.Properties.Add(AddressingProperty.Name, new AddressingProperty(message.Headers));
                }

                message.Headers.Action = null;
                message.Headers.To = null;
            }

            bool httpResponseMessagePropertyFound = responseProperty != null;

            string contentType = null;
            if (message.Version == MessageVersion.None && httpResponseMessagePropertyFound && !string.IsNullOrEmpty(responseProperty.Headers[HttpResponseHeader.ContentType]))
            {
                contentType = responseProperty.Headers[HttpResponseHeader.ContentType];
                responseProperty.Headers.Remove(HttpResponseHeader.ContentType);
                if (!messageEncoder.IsContentTypeSupported(contentType))
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                        new ProtocolException(SR.GetString(SR.ResponseContentTypeNotSupported,
                        contentType)));
                }
            }

            if (string.IsNullOrEmpty(contentType))
            {
                MtomMessageEncoder mtomMessageEncoder = messageEncoder as MtomMessageEncoder;
                if (mtomMessageEncoder == null)
                {
                    contentType = messageEncoder.ContentType;
                }
                else
                {
                    contentType = mtomMessageEncoder.GetContentType(out this.mtomBoundary);
                    // For MTOM messages, add a MIME version header
                    httpResponseMessage.Headers.Add(HttpChannelUtilities.MIMEVersionHeader, DefaultMimeVersion);
                }
            }

            if (isRequest && FxTrace.Trace.IsEnd2EndActivityTracingEnabled)
            {
                EnsureEventTraceActivity(message);
            }

            if (this.CanSendCompressedResponses)
            {
                string contentEncoding;
                string compressionContentType = contentType;
                if (HttpChannelUtilities.GetHttpResponseTypeAndEncodingForCompression(ref compressionContentType, out contentEncoding))
                {
                    contentType = compressionContentType;
                    this.SetContentEncoding(contentEncoding);
                }
            }

            if (httpResponseMessage.Content != null && !string.IsNullOrEmpty(contentType))
            {
                MediaTypeHeaderValue mediaTypeHeaderValue;
                if (!MediaTypeHeaderValue.TryParse(contentType, out mediaTypeHeaderValue))
                {
                    throw FxTrace.Exception.Argument("contentType", SR.GetString(SR.InvalidContentTypeError, contentType));
                }
                httpResponseMessage.Content.Headers.ContentType = mediaTypeHeaderValue;
            }

            bool httpMethodIsHead = string.Compare(this.HttpMethod, "HEAD", StringComparison.OrdinalIgnoreCase) == 0;

            if (httpMethodIsHead ||
                httpResponseMessagePropertyFound && responseProperty.SuppressEntityBody)
            {
                httpResponseMessage.Content.Headers.ContentLength = 0;
                httpResponseMessage.Content.Headers.ContentType = null;
            }

            if (httpResponseMessagePropertyFound)
            {
                httpResponseMessage.StatusCode = responseProperty.StatusCode;
                if (responseProperty.StatusDescription != null)
                {
                    responseProperty.StatusDescription = responseProperty.StatusDescription;
                }

                foreach (string key in responseProperty.Headers.AllKeys)
                {
                    httpResponseMessage.AddHeader(key, responseProperty.Headers[key]);
                }
            }

            if (!message.IsEmpty)
            {
                using (HttpContent content = httpResponseMessage.Content)
                {
//.........这里部分代码省略.........
开发者ID:mind0n,项目名称:hive,代码行数:101,代码来源:HttpChannelHelpers.cs

示例2: CreateResponseMessage

        private static HttpResponseMessage CreateResponseMessage(Message message, HttpResponseMessageProperty responseMessageProperty)
        {
            Fx.Assert(message != null, "The 'message' parameter should not be null.");
            Fx.Assert(responseMessageProperty != null, "The 'responseMessageProperty' parameter should not be null.");
            
            HttpResponseMessage response = new HttpResponseMessage();
            response.StatusCode = responseMessageProperty.HasStatusCodeBeenSet ?
                responseMessageProperty.StatusCode :
                message.IsFault ? HttpStatusCode.InternalServerError : HttpStatusCode.OK;

            string reasonPhrase = responseMessageProperty.StatusDescription;
            if (!string.IsNullOrEmpty(reasonPhrase))
            {
                response.ReasonPhrase = reasonPhrase;
            }

            response.Content = CreateMessageContent(message, responseMessageProperty.SuppressEntityBody);

            WebHeaderCollection headers = responseMessageProperty.Headers;
            foreach (string headerKey in headers.AllKeys)
            {
                response.AddHeader(headerKey, headers[headerKey]);
            }

            return response;
        }
开发者ID:nlh774,项目名称:DotNetReferenceSource,代码行数:26,代码来源:MessageExtensionMethods.cs


注:本文中的System.Net.Http.HttpResponseMessage.AddHeader方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。