本文整理汇总了C#中HttpListener.Close方法的典型用法代码示例。如果您正苦于以下问题:C# HttpListener.Close方法的具体用法?C# HttpListener.Close怎么用?C# HttpListener.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpListener
的用法示例。
在下文中一共展示了HttpListener.Close方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main()
{
Console.Title = "Samples.CustomChecks.3rdPartySystem";
Console.WriteLine("Press enter key to toggle the server to return a error or success");
Console.WriteLine("Press any key to exit");
using (listener = new HttpListener())
{
listener.Prefixes.Add("http://localhost:57789/");
listener.Start();
listener.BeginGetContext(ListenerCallback, listener);
while (true)
{
ConsoleKeyInfo key = Console.ReadKey();
Console.WriteLine();
if (key.Key != ConsoleKey.Enter)
{
return;
}
listener.Close();
if (isReturningOk)
{
Console.WriteLine("\r\nCurrently returning success");
}
else
{
Console.WriteLine("\r\nCurrently returning error");
}
isReturningOk = !isReturningOk;
}
}
}
示例2: Main
static void Main ()
{
HttpListener listener = new HttpListener ();
listener.IgnoreWriteExceptions = true;
listener.Prefixes.Add ("http://*:8081/");
listener.Start ();
listener.BeginGetContext (RequestHandler, listener);
while (true) {
Thread.Sleep (250);
if (File.Exists ("stop-server.tmp"))
break;
}
listener.Close ();
File.Create ("finish-server.tmp").Close ();
}
示例3: UrlPrefix
static UrlPrefix()
{
// Find a URL prefix that is not in use on this machine *and* uses a port that's not in use.
// Once we find this prefix, keep a listener on it for the duration of the process, so other processes
// can't steal it.
Guid processGuid = Guid.NewGuid();
for (int port = 1024; port <= IPEndPoint.MaxPort; port++)
{
string prefix = $"http://localhost:{port}/{processGuid:N}/";
var listener = new HttpListener();
try
{
listener.Prefixes.Add(prefix);
listener.Start();
s_processPrefixListener = listener;
s_processPrefix = prefix;
break;
}
catch (Exception e)
{
// can't use this prefix
listener.Close();
// Remember the exception for later
s_processPrefixException = e;
// If this is not an HttpListenerException, something very wrong has happened, and there's no point
// in trying again.
if (!(e is HttpListenerException))
break;
}
}
// At this point, either we've reserved a prefix, or we've tried everything and failed. If we failed,
// we've saved the exception for later. We'll defer actually *throwing* the exception until a test
// asks for the prefix, because dealing with a type initialization exception is not nice in xunit.
}