Reputation: 3
I want to make a http server for testing. I am using the HttpListener class for this purpose.
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://localhost:24578/");
while (true)
{
listener.Start();
HttpListenerContext context = listener.GetContext();
HandleRequest(context.Request);
string responseString = "<HTML><BODY><p>Test</br>Test new line</p></BODY></HTML>";
byte[] buffer = Encoding.UTF8.GetBytes(responseString);
context.Response.ContentLength64 = buffer.Length;
Stream output = context.Response.OutputStream;
output.Write(buffer, 0, buffer.Length);
output.Close();
listener.Stop();
}
By connecting to http://localhost:24578/ I can see my website working fine as you can see here
However, I would like to access my website from other devices connected to my network, so I opened cmd and typed ipconfig, my local ipv4 address is 192.168.1.108. So I opened in my browser http://192.168.1.108:24578/. It should be working fine as it's the same as localhost, but instead it shows me error 400 bad request, as shownhere
Also, the web server does not receive the request since it doesn't write anything in the console (the method HandleRequest writes info about the request, and is never called).
Upvotes: 0
Views: 262
Reputation: 2388
You explicitly said that webserver only accept connections from localhost. change this
listener.Prefixes.Add("http://localhost:24578/");
to
listener.Prefixes.Add("http://*:24578/");
You might need to run your application with administrator's right.
Upvotes: 1