I am trying to write a very simple custom http server in C #.
The code to achieve the connection is simple, as .Net suggests it:
IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
IPAddress theAddress = localIPs.Where(ip => ip.AddressFamily == AddressFamily.InterNetwork).FirstOrDefault();
var listener = new HttpListener();
string myUrlPrefix = $"http://{theAddress.ToString()}:{port}/MyService/";
listener.Prefixes.Add(myUrlPrefix);
Console.WriteLine($"Trying to listen on {myUrlPrefix}");
listener.Start();
Console.WriteLine("Listening...");
HttpListenerContext context = null;
try
{
context = listener.GetContext();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
HttpListenerRequest request = context.Request;
When doing this, the GetContext()blocks never return and throw an exception.
listener.Start() works -- the URL is properly reserved for the correct user with 'netsh http add urlacl...'., else Start() would throw. The URL is also only reserved once.
I see with netstat -na that the port is listening.
I am trying to access this either through a browser or through an cygwinimplementation wget. Both give me "ERROR 503: Service Unavailable.".
Here is the wget output:
$ wget http://127.0.0.1:45987/MyService/
--2016-03-06 14:54:37-- http://127.0.0.1:45987/MyService/
Connecting to 127.0.0.1:45987... connected.
HTTP request sent, awaiting response... 503 Service Unavailable
2016-03-06 14:54:37 ERROR 503: Service Unavailable.
, TCP , HttpListener.GetContext() , , , , , , . . First-Chance, .
Net Stackoverflow .
Windows .
Windows 10 Pro Microsoft Visual Studio Community 2015
14.0.23107.0 D14REL Microsoft.NET Framework 4.6.01038.
, , , .
- , GetContext() ?
======
: .Net Framework source stepping GetContext(). HTTP- "UnsafeNclNativeMethods.HttpApi.HttpReceiveHttpRequest", , , HttpReceiveHttpRequest, https://msdn.microsoft.com/en-us/library/windows/desktop/aa364495(v=vs.85).aspx. API-, .