forked from Crich1187/example-csharp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebserver.cs
More file actions
64 lines (54 loc) · 1.69 KB
/
webserver.cs
File metadata and controls
64 lines (54 loc) · 1.69 KB
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
58
59
60
61
62
63
64
using System;
using System.Net;
using System.Threading;
using System.Linq;
using System.Text;
namespace SimpleWebServer
{
public class WebServer
{
private readonly HttpListener _listener = new HttpListener();
public WebServer()
{
if (!HttpListener.IsSupported)
throw new NotSupportedException("Needs Windows XP SP2, Server 2003 or later.");
_listener.Prefixes.Add("http://*:8080/");
}
public void start()
{
_listener.Start();
for(;;)
{
HttpListenerContext ctx = _listener.GetContext();
new Thread(new Worker(ctx).ProcessRequest).Start();
}
}
public void Stop()
{
_listener.Stop();
_listener.Close();
}
static public void Main ()
{
WebServer webServer = new WebServer();
webServer.start();
}
}
public class Worker
{
private HttpListenerContext context;
public Worker(HttpListenerContext context)
{
this.context = context;
}
public void ProcessRequest()
{
StringBuilder sb = new StringBuilder();
sb.Append("<html><title>Puppet Pipelines CI/CD with Windows and C#</title><body><h1>Hello World from Puppet 12-7-2018.</h1><br><p>Build, test, and deploy your Windows apps with Pipelines.</p></body></html>");
byte[] b = Encoding.UTF8.GetBytes(sb.ToString());
context.Response.ContentLength64 = b.Length;
context.Response.OutputStream.Write(b, 0, b.Length);
context.Response.OutputStream.Close();
}
}
}