Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Net;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace selfHostedHttp
- {
- class Program
- {
- static void Main(string[] args)
- {
- var listener = new HttpListener();
- listener.Prefixes.Add("http://localhost:8080/");
- listener.Start();
- Console.WriteLine("Сервер начал прослушивание порта 8080");
- var path = GetPathHTMLFiles();
- try
- {
- while (true)
- {
- var context = listener.GetContext();
- var request = context.Request;
- var response = context.Response;
- var neededFile = request.Url.AbsolutePath;
- string sendedFile = null;
- if (neededFile == "/")
- neededFile = "/firstPage.html";
- else
- {
- if (File.Exists(path + neededFile))
- sendedFile = neededFile;
- else
- sendedFile = "/errorPage.html";
- }
- var bytes = File.ReadAllBytes(path + sendedFile);
- response.ContentLength64 = bytes.Length;
- var sw = response.OutputStream;
- sw.Write(bytes, 0, bytes.Length);
- sw.Close();
- }
- }
- catch
- {
- listener.Stop();
- Console.WriteLine("Сервер закончил прослушивание порта 8080");
- }
- }
- private static string GetPathHTMLFiles()
- {
- var path = GetParentSeveralTimes(4);
- path += "\\www_root";
- return path;
- }
- private static string GetParentSeveralTimes(int count)
- {
- var path = Environment.CurrentDirectory;
- for (var i = 0; i < count; i++)
- path = Directory.GetParent(path).ToString();
- return path;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement