.Net HttpListener.GetContext () provides the "503 Service Unavailable" to the client

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:

 // get our IPv4 address
 IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
 IPAddress theAddress = localIPs.Where(ip => ip.AddressFamily == AddressFamily.InterNetwork).FirstOrDefault();

 // Create a http listener.
 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-, .

+5
2

, .

, TcpListener, .

 // Create a Tcp listener.
 mTcpListener = new TcpListener(theAddress, port);

 Console.WriteLine($"Trying to listen on {theAddress}:{port}");
 mTcpListener.Start();
 Console.WriteLine("Listening...");
 Socket socket = null;
 try
 {
     socket = mTcpListener.AcceptSocket();
 }
 catch (Exception ex)
 {
     Console.WriteLine(ex.Message);
     return;
 }
 // wait a little for the socket buffer to fill up
 await Task.Delay(20);

 int bytesAvailable = socket.Available;
 var completeBuffer = new List<byte>();
 while (bytesAvailable > 0)
 {
     byte[] buffer = new byte[bytesAvailable];
     int bytesRead = socket.Receive(buffer);
     completeBuffer.AddRange(buffer.Take(bytesRead));

     bytesAvailable = socket.Available;
 }

 byte[] receivedBytes = completeBuffer.ToArray();

 string receivedString = Encoding.ASCII.GetString(receivedBytes);

... .

GET /MyService/ HTTP/1.1
User-Agent: Wget/1.17.1 (cygwin)
Accept: */*
Accept-Encoding: identity
Host: 127.0.0.1:45987
Connection: Keep-Alive

, HTTP ... , .

0

, ACL URL.

netsh http show urlacl ACL URL

netsh http delete urlacl http://+:1234/ ACL URL- http://+:1234/

netsh http add urlacl url=http://+:1234/ user=Everyone ACL URL-, ( )

URL- HttpListener.

localhost, URL ACL.

ACL URL . , , , 503 GetContext.

0

All Articles