Prosty serwer WWW, podstrony

0

Witam napisałem prosty serwer WWW w C#. Problemem jest to, iż nie wiem jak mam zrobić wczytywanie podstron. W zamyśle mam już koncepcje jak co i jak, a mianowicie będę wczytywał kod każdego pliku *.html do tablicy stringów ale nie wiem co potem z tym zrobić.

Kod:

using System;
using System.Text;
using System.IO;
using System.Net;

using SimpleWebServer;

namespace SimpleWebServer
{
	class MainClass
	{
		public static string HTMLfile;
		public static string com;

		public static string readHTMLfile()
		{
			string errFile = "<HTML><BODY>NO INDEX FILE</BODY></HTML>";
			string file;

			if (File.Exists (@"C:\WWW\index.html")) {
				file = File.ReadAllText(@"C:\WWW\index.html");	
				return file;
			} else {
				return errFile;
			}
		}

		public static string SR(HttpListenerRequest request)
		{
			return string.Format(HTMLfile);    
		}

		static void Main(string[] args)
		{

			HTMLfile = readHTMLfile ();
			Console.WriteLine ("HTML file loaded!");

			WebServer ws = new WebServer(SR, "http://localhost:8080/");
			ws.Run ();
			Console.WriteLine ("Server Status: ON");
		
			Console.ReadKey ();
			Console.WriteLine ("Server Status: OFF");
		}
	}
}
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();
		private readonly Func<HttpListenerRequest, string> _responderMethod;

		public WebServer(string[] prefixes, Func<HttpListenerRequest, string> method)
		{
			if (!HttpListener.IsSupported)
				throw new NotSupportedException(
					"Needs Windows XP SP2, Server 2003 or later.");

			if (prefixes == null || prefixes.Length == 0)
				throw new ArgumentException("prefixes");

			if (method == null)
				throw new ArgumentException("method");

			foreach (string s in prefixes)
				_listener.Prefixes.Add(s);

			_responderMethod = method;
			_listener.Start();
		}

		public WebServer(Func<HttpListenerRequest, string> method, params string[] prefixes)
			: this(prefixes, method) { }

		public void Run()
		{
			ThreadPool.QueueUserWorkItem((o) =>
				{
					try
					{
						while (_listener.IsListening)
						{
							ThreadPool.QueueUserWorkItem((c) =>
								{
									var ctx = c as HttpListenerContext;
									try
									{
										string rstr = _responderMethod(ctx.Request);
										byte[] buf = Encoding.UTF8.GetBytes(rstr);
										ctx.Response.ContentLength64 = buf.Length;
										ctx.Response.OutputStream.Write(buf, 0, buf.Length);
									}
									catch { }
									finally
									{
										ctx.Response.OutputStream.Close();
									}
								}, _listener.GetContext());
						}
					}
					catch { } 
				});
		}

		public void Stop()
		{
			_listener.Stop();
			_listener.Close();
		}
	}
} 
0

Potrzebne to do czegoś czy sam piszesz dla siebie? Pytam, bo wyważasz otwarte nie tyle drzwi, co furtkę płotka ogrodowego i to jeszcze taranem.

0

Ciekawe porównanie. Piszę dla siebie.

1 użytkowników online, w tym zalogowanych: 0, gości: 1