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


C# HttpListenerContext.GetHttpRequestMessage方法代码示例

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


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

示例1: ProcessRequestAsync

        private Task ProcessRequestAsync(HttpListenerContext context)
        {
            try
            {
                Debug.WriteLine("Server: Incoming request to {0}.", context.Request.Url);

                PersistentConnection connection;

                string path = ResolvePath(context.Request.Url);

                foreach (var embeddedFileHandler in _embeddedFileHandlers)
                {
                    var result = embeddedFileHandler.Handle(path, context);
                    if (result != null)
                    {
                        return result;
                    }
                }

                if (_routingHost.TryGetConnection(path, out connection))
                {
                    // https://developer.mozilla.org/En/HTTP_Access_Control
                    string origin = context.Request.Headers["Origin"];
                    if (!String.IsNullOrEmpty(origin))
                    {
                        context.Response.AddHeader("Access-Control-Allow-Origin", origin);
                        context.Response.AddHeader("Access-Control-Allow-Credentials", "true");
                    }

                    var request = new HttpListenerRequestWrapper(context);
                    var response = new HttpListenerResponseWrapper(context.Response, _disconnectHandler.GetDisconnectToken(context));
                    var hostContext = new HostContext(request, response);

            #if NET45
                    hostContext.Items[HostConstants.SupportsWebSockets] = Environment.OSVersion.Version.Major >= 6 && Environment.OSVersion.Version.Minor >= 2;
            #endif

                    if (OnProcessRequest != null)
                    {
                        OnProcessRequest(hostContext);
                    }

            #if DEBUG
                    hostContext.Items[HostConstants.DebugMode] = true;
            #endif
                    hostContext.Items["System.Net.HttpListenerContext"] = context;

                    // Initialize the connection
                    connection.Initialize(_routingHost.DependencyResolver);

                    return connection.ProcessRequestAsync(hostContext);
                }

                if (path.Equals("/clientaccesspolicy.xml", StringComparison.InvariantCultureIgnoreCase))
                {
                    using (var stream = typeof(WebAppServer).Assembly.GetManifestResourceStream(typeof(WebAppServer), "clientaccesspolicy.xml"))
                    {
                        if (stream == null)
                        {
                            var response = new HttpResponseMessage(HttpStatusCode.NotFound);
                            return context.SendResponseAsync(response);
                        }
                        var bytes = new byte[1024];
                        int byteCount = stream.Read(bytes, 0, bytes.Length);
                        return context.Response.WriteAsync(new ArraySegment<byte>(bytes, 0, byteCount));
                    }
                }

                HttpRequestMessage requestMessage = context.GetHttpRequestMessage();

                return _webApiServer.PublicSendAsync(requestMessage, _disconnectHandler.GetDisconnectToken(context))
                    .Then(response =>
                    {
                        var responseMessage = response ?? new HttpResponseMessage(HttpStatusCode.InternalServerError) { RequestMessage = requestMessage };
                        return context.SendResponseAsync(responseMessage);
                    });
            }
            catch (Exception ex)
            {
                return TaskAsyncHelper.FromError(ex);
            }
        }
开发者ID:jkells,项目名称:WebAppHost,代码行数:82,代码来源:WebAppServer.cs


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