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