-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpUtils.cs
More file actions
35 lines (28 loc) · 1.35 KB
/
HttpUtils.cs
File metadata and controls
35 lines (28 loc) · 1.35 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
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace languages
{
public static class HttpUtils
{
internal const string mainPage = "/index.html";
internal const string ResponseTemplate = "<HTML><BODY>{0}</BODY></HTML>";
internal const string ResponseWithScriptTemplate = "<HTML>" + ScriptTemplate + "<BODY>{1}</BODY></HTML>";
internal const string ScriptTemplate = "<script>{0}</script>";
internal const string redirectTimerScript = "setTimeout(function () {window.location.href = '" + mainPage + "';}, 3000);";
internal static async Task WriteResponseTextAsync(this HttpListenerResponse response, string text)
{
string responsePayload = string.Format(HttpUtils.ResponseTemplate, text);
var responseBytes = Encoding.UTF8.GetBytes(responsePayload);
await response.OutputStream.WriteAsync(responseBytes);
}
internal static async Task JsRedirectWithMessageAsync(this HttpListenerResponse response, string text)
{
string responsePayload = string.Format(HttpUtils.ResponseWithScriptTemplate,
HttpUtils.redirectTimerScript,
text);
var responseBytes = Encoding.UTF8.GetBytes(responsePayload);
await response.OutputStream.WriteAsync(responseBytes);
}
}
}