本文整理汇总了C#中HttpServer.Start方法的典型用法代码示例。如果您正苦于以下问题:C# HttpServer.Start方法的具体用法?C# HttpServer.Start怎么用?C# HttpServer.Start使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpServer
的用法示例。
在下文中一共展示了HttpServer.Start方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Second_start_on_different_port_must_fail
public void Second_start_on_different_port_must_fail()
{
const int port = 8080;
using (var server = new HttpServer())
{
server.Start(port, null);
Executing.This(() => server.Start(port + 1, null))
.Should()
.Throw<AtlasException>()
.And.Exception.ErrorCode.Should().Be.EqualTo(HttpServerErrors.ServerAlreadyStarted);
}
}
示例2: Main
public static void Main(string[] args)
{
_httpsv = new HttpServer(4649);
//_httpsv.Sweeping = false;
_httpsv.AddWebSocketService<Echo>("/Echo");
_httpsv.AddWebSocketService<Chat>("/Chat");
_httpsv.OnGet += (sender, e) =>
{
onGet(e);
};
_httpsv.OnError += (sender, e) =>
{
Console.WriteLine(e.Message);
};
_httpsv.Start();
Console.WriteLine("HTTP Server listening on port: {0} service path:", _httpsv.Port);
foreach (var path in _httpsv.ServicePaths)
Console.WriteLine(" {0}", path);
Console.WriteLine();
Console.WriteLine("Press any key to stop server...");
Console.ReadLine();
_httpsv.Stop();
}
示例3: PostWithContentWithoutType
public void PostWithContentWithoutType()
{
using (var server = new HttpServer())
using (var uploadStream = new MemoryStream())
{
WriteRandomData(uploadStream, 1 * 1024 * 1024);
server.RequestReceived += (s, e) =>
{
uploadStream.Position = 0;
Assert.AreEqual("POST", e.Request.HttpMethod);
Assert.AreEqual(uploadStream, e.Request.InputStream);
Assert.IsNull(e.Request.ContentType);
};
server.Start();
var request = (HttpWebRequest)WebRequest.Create(
String.Format("http://{0}/", server.EndPoint)
);
uploadStream.Position = 0;
request.Method = "POST";
request.ContentLength = uploadStream.Length;
using (var stream = request.GetRequestStream())
{
uploadStream.CopyTo(stream);
}
GetResponseFromRequest(request);
}
}
示例4: ForcedShutdown
public void ForcedShutdown()
{
using (var server = new HttpServer())
{
server.ShutdownTimeout = TimeSpan.FromSeconds(1);
server.RequestReceived += (s, e) =>
{
// Start closing the server.
ThreadPool.QueueUserWorkItem(p => server.Stop());
// Wait some time to fulfill the request.
Thread.Sleep(TimeSpan.FromSeconds(30));
using (var writer = new StreamWriter(e.Response.OutputStream))
{
writer.WriteLine("Hello!");
}
};
server.Start();
var request = (HttpWebRequest)WebRequest.Create(
String.Format("http://{0}/", server.EndPoint)
);
GetResponseFromRequest(request);
}
}
示例5: Main
public static void Main(string [] args)
{
_httpsv = new HttpServer (4649);
#if DEBUG
_httpsv.Log.Level = LogLevel.TRACE;
#endif
_httpsv.RootPath = ConfigurationManager.AppSettings ["RootPath"];
//_httpsv.KeepClean = false;
_httpsv.AddWebSocketService<Echo> ("/Echo");
_httpsv.AddWebSocketService<Chat> ("/Chat");
_httpsv.OnGet += (sender, e) =>
{
onGet (e);
};
_httpsv.OnError += (sender, e) =>
{
Console.WriteLine (e.Message);
};
_httpsv.Start ();
Console.WriteLine ("HTTP Server listening on port: {0} service path:", _httpsv.Port);
foreach (var path in _httpsv.ServicePaths)
Console.WriteLine (" {0}", path);
Console.WriteLine ();
Console.WriteLine ("Press enter key to stop the server...");
Console.ReadLine ();
_httpsv.Stop ();
}
示例6: Main
public static void Main(string [] args)
{
_httpsv = new HttpServer (4649);
//_httpsv = new HttpServer (4649, true);
#if DEBUG
_httpsv.Log.Level = LogLevel.TRACE;
#endif
_httpsv.RootPath = ConfigurationManager.AppSettings ["RootPath"];
//var certFile = ConfigurationManager.AppSettings ["ServerCertFile"];
//var password = ConfigurationManager.AppSettings ["CertFilePassword"];
//_httpsv.Certificate = new X509Certificate2 (certFile, password);
//_httpsv.KeepClean = false;
_httpsv.AddWebSocketService<Echo> ("/Echo");
_httpsv.AddWebSocketService<Chat> ("/Chat");
_httpsv.OnGet += (sender, e) =>
{
onGet (e);
};
_httpsv.Start ();
if (_httpsv.IsListening)
{
Console.WriteLine ("HTTP Server listening on port: {0} service path:", _httpsv.Port);
foreach (var path in _httpsv.WebSocketServices.ServicePaths)
Console.WriteLine (" {0}", path);
Console.WriteLine ();
}
Console.WriteLine ("Press Enter key to stop the server...");
Console.ReadLine ();
_httpsv.Stop ();
}
示例7: HandledHandlerExceptionWithoutOutput
public void HandledHandlerExceptionWithoutOutput()
{
using (var server = new HttpServer())
{
server.RequestReceived += (s, e) =>
{
throw new Exception();
};
server.UnhandledException += (s, e) =>
{
e.Response.Status = "200 OK";
e.Handled = true;
};
server.Start();
var request = (HttpWebRequest)WebRequest.Create(
String.Format("http://{0}/", server.EndPoint)
);
using (var response = (HttpWebResponse)request.GetResponse())
{
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
Assert.AreEqual("OK", response.StatusDescription);
using (var stream = response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
Assert.IsEmpty(reader.ReadToEnd());
}
}
}
}
示例8: ExceptionOnRequestReceived
public void ExceptionOnRequestReceived()
{
using (var server = new HttpServer())
{
server.RequestReceived += (s, e) =>
{
throw new Exception();
};
server.Start();
var request = (HttpWebRequest)WebRequest.Create(
String.Format("http://{0}/", server.EndPoint)
);
try
{
GetResponseFromRequest(request);
}
catch (WebException ex)
{
var response = (HttpWebResponse)ex.Response;
Assert.AreEqual(HttpStatusCode.InternalServerError, response.StatusCode);
Assert.AreEqual("Internal Server Error", response.StatusDescription);
}
}
}
示例9: Main
static void Main(string[] args)
{
HttpServer http = new HttpServer();
http.ProcessRequest = (request, response) =>
{
Console.WriteLine(request.Url);
if (request.Url != "/foo")
{
response.WriteLine("Hello from {0}.", request.Url);
}
else
{
response.StatusCode = HttpStatusCode.NotFound;
}
};
http.Start();
Console.WriteLine("Press enter to stop HTTP server.");
Console.ReadLine();
http.Stop();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
示例10: Issue14_should_work_with_concurrent_requests
public Issue14_should_work_with_concurrent_requests()
{
var moduleManager = new ModuleManager();
moduleManager.Add(this);
_server = new HttpServer(moduleManager);
_server.Start(IPAddress.Any, 0);
}
示例11: QueryStringKeyWithMultipleValues
public void QueryStringKeyWithMultipleValues()
{
using (var server = new HttpServer())
{
server.RequestReceived += (s, e) =>
{
Assert.That(e.Request.QueryString.AllKeys, Is.EquivalentTo(new[] { "key" }));
Assert.That(e.Request.QueryString.GetValues("key"), Is.EquivalentTo(new[] { "first", "second", "third" }));
Assert.AreEqual(e.Request.QueryString["key"], "first,second,third");
using (var writer = new StreamWriter(e.Response.OutputStream))
{
writer.Write(ResponseText);
}
};
server.Start();
var request = (HttpWebRequest)WebRequest.Create(
String.Format("http://{0}/?key=first&key=second&key=third", server.EndPoint)
);
Assert.AreEqual(ResponseText, GetResponseFromRequest(request));
}
}
示例12: BasicUpload
public void BasicUpload()
{
using (var server = new HttpServer())
{
server.RequestReceived += (s, e) =>
{
Assert.That(e.Request.Form.AllKeys, Is.EquivalentTo(new[] { "key" }));
Assert.AreEqual("value", e.Request.Form["key"]);
Assert.AreEqual("multipart/form-data", e.Request.ContentType);
Assert.AreEqual("POST", e.Request.HttpMethod);
Assert.IsNull(e.Request.InputStream);
};
server.Start();
var request = (HttpWebRequest)WebRequest.Create(
String.Format("http://{0}/", server.EndPoint)
);
using (var writer = new MultiPartWriter(request))
{
writer.WriteValue("key", "value");
}
GetResponseFromRequest(request);
}
}
示例13: SetCookie
public void SetCookie()
{
using (var server = new HttpServer())
{
server.RequestReceived += (s, e) =>
{
e.Response.Cookies["a"].Value = "b";
};
server.Start();
var request = (HttpWebRequest)WebRequest.Create(
String.Format("http://{0}/", server.EndPoint)
);
request.CookieContainer = new CookieContainer();
using (var response = (HttpWebResponse)request.GetResponse())
{
Assert.AreEqual("a=b; path=/", response.Headers["Set-Cookie"]);
Assert.AreEqual(1, response.Cookies.Count);
var cookie = response.Cookies[0];
Assert.AreEqual("a", cookie.Name);
Assert.AreEqual("b", cookie.Value);
Assert.AreEqual("/", cookie.Path);
Assert.AreEqual(server.EndPoint.Address.ToString(), cookie.Domain);
Assert.AreEqual(DateTime.MinValue, cookie.Expires);
Assert.AreEqual(false, cookie.HttpOnly);
Assert.AreEqual(false, cookie.Secure);
}
}
}
示例14: Main
static void Main(string[] args)
{
// Template generators are used to render templates
// (convert code + html to pure html).
TemplateManager mgr = new TemplateManager();
mgr.Add("haml", new HamlGenerator());
// The httpserver is quite dumb and will only serve http, nothing else.
HttpServer server = new HttpServer();
// a controller mode implements a MVC pattern
// You'll add all controllers to the same module.
ControllerModule mod = new ControllerModule();
mod.Add(new UserController(mgr));
server.Add(mod);
// file module will be handling files
FileModule fh = new FileModule("/", Environment.CurrentDirectory);
fh.AddDefaultMimeTypes();
server.Add(fh);
// Let's start pure HTTP, we can also start a HTTPS listener.
server.Start(IPAddress.Any, 8081);
Console.ReadLine();
}
示例15: HandledHandlerException
public void HandledHandlerException()
{
using (var server = new HttpServer())
{
server.RequestReceived += (s, e) =>
{
throw new Exception();
};
server.UnhandledException += (s, e) =>
{
e.Response.Status = "200 OK";
using (var writer = new StreamWriter(e.Response.OutputStream))
{
writer.Write(ErrorMessage);
}
};
server.Start();
var request = (HttpWebRequest)WebRequest.Create(
String.Format("http://{0}/", server.EndPoint)
);
Assert.AreEqual(ErrorMessage, GetResponseFromRequest(request));
}
}