However, when I add some madeup domain, like "whateverabc.com", the server is not responding to requests anymore and chrome is printing ERR_NAME_NOT_RESOLVED error.
What I am missing? Thanks!
public class WebServer
{
private readonly HttpListener _listener = new HttpListener();
private readonly Func
public WebServer(string[] prefixes, Func
{
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);
_listener.IgnoreWriteExceptions = true;
_responderMethod = method;
_listener.Start();
}
public WebServer(Func
: this(prefixes, method) { }
public void Run()
{
ThreadPool.QueueUserWorkItem((o) =>
{
Console.WriteLine("Webserver running...");
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 { } // suppress any exceptions
finally
{
ctx.Response.OutputStream.Close();
}
}, _listener.GetContext());
}
}
catch { } // suppress any exceptions
});
}
public void Stop()
{
_listener.Stop();
_listener.Close();
}
}
static void Main(string[] args)
{
WebServer ws = new WebServer(SendResponse, "http://whateverabc.com:54785/");
ws.Run();
Console.WriteLine("A simple webserver. Press a key to quit.");
Console.ReadKey();
ws.Stop();
}
public static string SendResponse(HttpListenerRequest request)
{
return string.Format("My web page.
{0}", DateTime.Now);
}

Michal HabalcikPosted Jun 19, 2017, 12:50 AM
Rajkiran SwainPosted Jun 18, 2017, 4:27 AM
Manikandan MurugesanPosted Jun 17, 2017, 1:29 PM
Manikandan MurugesanPosted Jun 17, 2017, 1:28 PM