コード例 #1
0
        public static void Main(string[] args)
        {
            int port = 8080;
            if (args.Length > 0)
            {
                if (!int.TryParse (args [0], out port)) {
                    Console.WriteLine ("{0} is not a valid port number", args [0]);
                    return;
                }
            }

            // Create an http server that will listen on port 80
            _httpHandler = new HttpHandler (port);

            // Install a couple content services
            var hello = new HelloWorldContentService ();
            _httpHandler.AddService ("/", hello);
            _httpHandler.AddService ("/hello", hello);
            _httpHandler.AddService ("/time", new TimeContentService ());

            // Start the http server
            _httpHandler.Start ();

            // If you haven't provided a content service that will call 'Stop', then
            //   this will wait forever.
            _httpHandler.WaitForExit ();
        }
コード例 #2
0
 public Task ProcessRequest(HttpHandler httpHandler, HttpListenerContext ctx)
 {
     var method = ctx.Request.HttpMethod.ToLowerInvariant ();
     switch (method) {
     case "get":
         return GetHandler (httpHandler, ctx);
     case "put":
         return PutHandler (httpHandler, ctx);
     case "delete":
         return DeleteHandler (httpHandler, ctx);
     case "patch":
         return PatchHandler (httpHandler, ctx);
     default:
         throw new NotImplementedException ();
     }
 }
コード例 #3
0
        public Task GetHandler(HttpHandler httpHandler, HttpListenerContext ctx)
        {
            var sb = new StringBuilder();
            sb.AppendLine ("<!DOCTYPE html PUBLIC \"-//IETF//DTD HTML 2.0//EN\">");
            sb.AppendLine ("<HTML>");
            sb.AppendLine ("  <HEAD>");
            sb.AppendLine ("    <TITLE>Hi</TITLE>");
            sb.AppendLine ("  </HEAD>");
            sb.AppendLine ("  <BODY>");
            sb.AppendLine ("    <H1>Hello World!</H1>");
            sb.AppendLine ("  </BODY>");
            sb.AppendLine ("</HTML>");

            var data = Encoding.UTF8.GetBytes(sb.ToString());
            ctx.Response.ContentLength64 = data.Length;
            ctx.Response.ContentType = "text/html";
            ctx.Response.OutputStream.Write(data, 0, data.Length);
            ctx.Response.OutputStream.Close();

            return Task.FromResult(0);
        }
コード例 #4
0
        public Task GetHandler(HttpHandler httpHandler, HttpListenerContext ctx)
        {
            var sb = new StringBuilder();
            sb.AppendLine ("<!DOCTYPE html PUBLIC \"-//IETF//DTD HTML 2.0//EN\">");
            sb.AppendLine ("<HTML>");
            sb.AppendLine ("  <HEAD>");
            sb.AppendLine ("    <TITLE>Current Time</TITLE>");
            sb.AppendLine ("  </HEAD>");
            sb.AppendLine ("  <BODY>");
            sb.AppendLine (string.Format("    <H1>The current UTC time is : {0}</H1>",
                                         DateTime.UtcNow.ToLongTimeString()));
            sb.AppendLine ("  </BODY>");
            sb.AppendLine ("</HTML>");

            var data = Encoding.UTF8.GetBytes(sb.ToString());
            ctx.Response.ContentLength64 = data.Length;
            ctx.Response.ContentType = "text/html";
            ctx.Response.OutputStream.Write(data, 0, data.Length);
            ctx.Response.OutputStream.Close();

            return Task.FromResult(0);
        }
コード例 #5
0
        public Task GetHandler(HttpHandler httpHandler, HttpListenerContext ctx)
        {
            var sb = new StringBuilder();

            sb.AppendLine("<!DOCTYPE html PUBLIC \"-//IETF//DTD HTML 2.0//EN\">");
            sb.AppendLine("<HTML>");
            sb.AppendLine("  <HEAD>");
            sb.AppendLine("    <TITLE>Current Time</TITLE>");
            sb.AppendLine("  </HEAD>");
            sb.AppendLine("  <BODY>");
            sb.AppendLine(string.Format("    <H1>The current UTC time is : {0}</H1>",
                                        DateTime.UtcNow.ToLongTimeString()));
            sb.AppendLine("  </BODY>");
            sb.AppendLine("</HTML>");

            var data = Encoding.UTF8.GetBytes(sb.ToString());

            ctx.Response.ContentLength64 = data.Length;
            ctx.Response.ContentType     = "text/html";
            ctx.Response.OutputStream.Write(data, 0, data.Length);
            ctx.Response.OutputStream.Close();

            return(Task.FromResult(0));
        }
コード例 #6
0
 public Task PutHandler(HttpHandler httpHandler, HttpListenerContext ctx)
 {
     throw new NotSupportedException ();
 }
コード例 #7
0
 public Task PatchHandler(HttpHandler httpHandler, HttpListenerContext ctx)
 {
     throw new NotSupportedException();
 }
コード例 #8
0
 private Task PutHandler(HttpHandler httpHandler, HttpListenerContext ctx)
 {
     return _service.PutHandler (httpHandler, ctx);
 }
コード例 #9
0
 private Task PatchHandler(HttpHandler httpHandler, HttpListenerContext ctx)
 {
     return(_service.PatchHandler(httpHandler, ctx));
 }
コード例 #10
0
 private Task DeleteHandler(HttpHandler httpHandler, HttpListenerContext ctx)
 {
     return(_service.DeleteHandler(httpHandler, ctx));
 }