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


C# StreamContent.ReadAsHttpRequestMessageAsync方法代码示例

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


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

示例1: Parse

        public static HttpMessage Parse(string data)
        {
            var httpMessage = new HttpMessage();

            HttpContent content;
            using (var sr = new StringReader(data))
            {
                // First line of data is (request|response) followed by a GUID to link request to response
                // Rest of data is in message/http format

                var firstLine = sr.ReadLine().Split(':');
                if (firstLine.Length < 2)
                {
                    throw new ArgumentException("Invalid formatted event :" + data);
                }
                httpMessage.IsRequest = firstLine[0] == "request";
                httpMessage.MessageId = Guid.Parse(firstLine[1]);

                var stream = new MemoryStream(Encoding.UTF8.GetBytes(sr.ReadToEnd()));
                stream.Position = 0;
                content = new StreamContent(stream);
            }

            var contentType = new MediaTypeHeaderValue("application/http");
            content.Headers.ContentType = contentType;

            if (httpMessage.IsRequest)
            {
                contentType.Parameters.Add(new NameValueHeaderValue("msgtype", "request"));

                // Using .Result isn't too evil because content is a locally buffered memory stream
                // Although if this were hosted in a System.Web based ASP.NET host it might block
                httpMessage.HttpRequestMessage = content.ReadAsHttpRequestMessageAsync().Result;
            }
            else
            {
                contentType.Parameters.Add(new NameValueHeaderValue("msgtype", "response"));
                httpMessage.HttpResponseMessage = content.ReadAsHttpResponseMessageAsync().Result;
            }
            return httpMessage;
        }
开发者ID:darrelmiller,项目名称:ApimEventProcessor,代码行数:41,代码来源:HttpMessage.cs


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