1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| using System;
using System.IO;
using System.Text;
using System.Diagnostics;
using HybridDSP.Net.HTTP;
namespace ExampleServer
{
class DateTimeHandler : IHTTPRequestHandler
{
private RequestHandlerFactory.WriteLogDelegate _writeLogDelegate;
public DateTimeHandler(RequestHandlerFactory.WriteLogDelegate d)
{
_writeLogDelegate = d;
}
#region IHTTPRequestHandler Members
public void HandleRequest(HTTPServerRequest request, HTTPServerResponse response)
{
if (_writeLogDelegate != null)
_writeLogDelegate("Handling DateTime request");
if (request.URI == "/")
{
/**
* In this example we'll write the body into the
* stream obtained by response.Send(). This will cause the
* KeepAlive to be false since the size of the body is not
* known when the response header is sent.
**/
response.ContentType = "text/html";
using (Stream ostr = response.Send())
using (TextWriter tw = new StreamWriter(ostr))
{
tw.WriteLine("<html>");
tw.WriteLine("<head>");
tw.WriteLine("<title>Date Time Server</title>");
tw.WriteLine("<meta http-equiv=\"refresh\" content=\"2\">");
tw.WriteLine("</header>");
tw.WriteLine("<body>");
tw.WriteLine(DateTime.Now.ToString());
tw.WriteLine("</body>");
tw.WriteLine("</html>");
}
}
else
{
response.StatusAndReason = HTTPServerResponse.HTTPStatus.HTTP_NOT_FOUND;
response.Send();
}
}
#endregion
}
} |
Partager