本文整理汇总了C#中HttpListener.EndGetContext方法的典型用法代码示例。如果您正苦于以下问题:C# HttpListener.EndGetContext方法的具体用法?C# HttpListener.EndGetContext怎么用?C# HttpListener.EndGetContext使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpListener
的用法示例。
在下文中一共展示了HttpListener.EndGetContext方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
db = new SQLiteConnection(ServerSettings.Default.ConnectionString);
db.Open();
CreateTables();
bool running = true;
Console.CancelKeyPress += (s, e) =>
{
running = false;
e.Cancel = true;
};
Console.WriteLine("Press Ctrl+C to quit.");
using (var http = new HttpListener())
{
http.Prefixes.Add(ServerSettings.Default.Prefix);
http.Start();
while (running)
{
HttpListenerContext context = null;
try
{
var result = http.BeginGetContext(null, null);
while (!result.IsCompleted)
{
if (running == false)
goto quit;
Thread.Sleep(0);
}
context = http.EndGetContext(result);
switch (context.Request.HttpMethod)
{
case "POST":
DispatchPost(context);
break;
default:
DispatchGet(context);
break;
}
}
catch (Exception ex)
{
using (var sw = new StreamWriter(context.Response.OutputStream, Encoding.UTF8))
{
sw.Write(ex.ToString());
}
context.Response.StatusCode = 500;
}
finally
{
if (context != null)
context.Response.Close();
}
}
quit:
db.Close();
}
}