-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMauiProgram.cs
108 lines (95 loc) · 3.3 KB
/
MauiProgram.cs
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System.Net;
using Microsoft.Extensions.Logging;
namespace DesktopDocumentSigner
{
public static class MauiProgram
{
private const string listenOn = "http://localhost:5000/";
private static HttpListener _httpListener = new HttpListener();
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
StartHttpServer();
#if DEBUG
builder.Logging.AddDebug();
#endif
return builder.Build();
}
private async static void StartHttpServer()
{
_httpListener.Prefixes.Add(listenOn);
try
{
_httpListener.Start();
while (true)
{
var context = await _httpListener.GetContextAsync();
HandleRequest(context);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
private async static void HandleRequest(HttpListenerContext context)
{
try
{
var request = context.Request;
var response = context.Response;
if (request.HttpMethod == "POST")
{
HandlePostRequest(request, response);
}
else
{
HandleDefaultRequest(response);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error handling request: {ex.Message}");
}
}
private static void HandleDefaultRequest(HttpListenerResponse response)
{
string responseString = "Pink OK!";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
response.ContentLength64 = buffer.Length;
using (var output = response.OutputStream)
{
output.Write(buffer, 0, buffer.Length);
}
}
private static void HandlePostRequest(HttpListenerRequest request, HttpListenerResponse response)
{
try
{
using (var reader = new StreamReader(request.InputStream, request.ContentEncoding))
{
string requestBody = reader.ReadToEnd();
Console.WriteLine($"Received POST data: {requestBody}");
string responseString = $"Received your POST data: {requestBody}";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
response.ContentLength64 = buffer.Length;
using (var output = response.OutputStream)
{
output.Write(buffer, 0, buffer.Length);
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error processing POST request: {ex.Message}");
}
}
}
}