本文整理汇总了C#中HttpListener.BeginGetContext方法的典型用法代码示例。如果您正苦于以下问题:C# HttpListener.BeginGetContext方法的具体用法?C# HttpListener.BeginGetContext怎么用?C# HttpListener.BeginGetContext使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpListener
的用法示例。
在下文中一共展示了HttpListener.BeginGetContext方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: StartListener
static void StartListener()
{
listener = new HttpListener();
listener.Prefixes.Add("http://localhost:57789/");
listener.Start();
listener.BeginGetContext(ListenerCallback, listener);
}
示例3: Awake
void Awake()
{
listener = new HttpListener();
listener.Prefixes.Add("http://*:7663/");
listener.Start();
listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);
}
示例4: Listener
public Listener ()
{
EndPoints = new List<IPEndPoint> ();
listener = new HttpListener ();
listener.Prefixes.Add (KeepAliveTest.URL);
listener.Start ();
listener.BeginGetContext (Handle, null);
}
示例5: 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 ();
}
示例6: Main
static int Main ()
{
HttpListener listener = new HttpListener ();
listener.Prefixes.Add ("http://" + IPAddress.Loopback.ToString () + ":8001/");
listener.Start ();
IAsyncResult result = listener.BeginGetContext (new AsyncCallback (ListenerCallback), listener);
HttpWebRequest request = (HttpWebRequest) WebRequest.Create ("http://" + IPAddress.Loopback.ToString () + ":8001/?a=1&b=2");
request.Accept = "image/png,text/html";
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse) request.GetResponse ();
response.Close ();
result.AsyncWaitHandle.WaitOne ();
return exitCode;
}
示例7: RemotingHttpListener
public RemotingHttpListener (IPAddress addr, int port, HttpServerTransportSink sink)
{
this.sink = sink;
bool find_port = false;
if (port == 0)
find_port = true;
string address = null;
if (addr == IPAddress.Any)
address = "*";
#if NET_2_0
else if (addr == IPAddress.IPv6Any)
address = "*";
#endif
else
address = addr.ToString ();
listener = new HttpListener ();
while (true) {
Random rnd = null;
if (find_port) {
if (rnd == null)
rnd = new Random ();
port = rnd.Next (1025, 65000);
}
try {
listener.Prefixes.Add (String.Format ("http://{0}:{1}/", address, port));
listener.Start ();
local_port = port;
break;
} catch (Exception e) {
if (!find_port)
throw;
listener.Prefixes.Clear ();
// Port already in use
}
}
listener.BeginGetContext (new AsyncCallback (OnGetContext), null);
}
示例8: 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();
}
}
示例9: Start
// Use this for initialization
void Start()
{
_listener = new HttpListener();
_listener.Prefixes.Add ("http://127.0.0.1:8080/");
_listener.Start();
_listener.BeginGetContext(new AsyncCallback(ListenerCallback),_listener);
}
示例10: Main
static void Main(string[] args)
{
uriBaseAddress = args[0];
if (!uriBaseAddress.EndsWith("/"))
uriBaseAddress += "/";
useV2Host = false;
if (args.Length > 1)
useV2Host = bool.Parse(args[1]);
string waitHandleName = null;
if (args.Length > 2)
waitHandleName = args[2];
serviceType = Assembly.GetExecutingAssembly()
.GetTypes()
.Single(t => !t.IsAbstract
&& typeof(Microsoft.OData.Service.IRequestHandler).IsAssignableFrom(t));
HttpListener listener = new HttpListener();
try
{
listener.Prefixes.Add(uriBaseAddress);
listener.Start();
listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);
}
catch (Exception e)
{
Console.WriteLine("An exception occurred:");
Console.WriteLine(e.ToString());
listener.Abort();
Console.ReadLine();
}
// both of the following cases will block, so there is no need for a loop
if (waitHandleName == "Debug" || string.IsNullOrEmpty(waitHandleName))
{
Console.WriteLine("Running in Debug mode, please press any key to exit");
// blocks until key pressed
Console.Read();
}
else
{
// if the wait handle name was specified on the command line, then the test infrastructure
// must have created one for us to wait on
// we do this instead of blocking on the command line because it has proven to be more robust
// and much easier to shut down the service when disposing the workspace
EventWaitHandle waitHandle = EventWaitHandle.OpenExisting(waitHandleName);
// blocks until the wait handle is triggered by the test infrastructure
waitHandle.WaitOne();
}
}