C#http侦听器不在本地主机上侦听,仅适用于FQDN

Mat*_*adt 5 c# http httplistener

(请删除接近的表决票。恕我直言,这是一个有效的问题,提供了所有必要的信息,但尝试提供所有必需的信息作为简要信息,以识别问题的可能根源)。

我是Java世界的C#新手。我尝试获得一个简单的http服务器来监听本地主机上的端口(在我的情况下为8080)。

我关注了这篇博客文章https://codehosting.net/blog/BlogEngine/post/Simple-C-Web-Server.aspx

当我运行程序时,它说

在此处输入图片说明

但我无法通过浏览器访问http:// localhost:8080 / test /netstat也不显示端口8888。该端口也没有被防火墙阻止。

通常,我可以在该端口上运行其他服务器,例如Tomcat,而不会出现问题。当我运行C#http服务器程序时,该端口未被其他应用程序阻止或使用。

代码:

Program.cs:

static void Main(string[] args)
{
    WebServer ws = new WebServer();
    ws.Run();
    Console.WriteLine("A simple webserver. Press a key to quit.");
    Console.ReadKey();
    ws.Stop();  
}
Run Code Online (Sandbox Code Playgroud)

WebServer.cs:

namespace WebServerTest
{
    public class WebServer
    {
        private readonly HttpListener _listener = new HttpListener();
        private readonly Func<HttpListenerRequest, string> _responderMethod;

        public WebServer()
        {
            if (!HttpListener.IsSupported)
                throw new NotSupportedException(
                    "Needs Windows XP SP2, Server 2003 or later.");

            _listener.Prefixes.Add("http://localhost:8080/test/");
            _responderMethod = SendResponse;
            _listener.Start();
        }

        public static string SendResponse(HttpListenerRequest request)
        {
            return string.Format("<HTML><BODY>My web page.<br>{0}</BODY></HTML>", DateTime.Now);
        }

        public void Run()
        {
            ThreadPool.QueueUserWorkItem((o) =>
            {
                Console.WriteLine("Webserver running...");

                    while (_listener.IsListening)
                    {
                        ThreadPool.QueueUserWorkItem((c) =>
                        {
                            var ctx = c as HttpListenerContext;

                            string rstr = _responderMethod(ctx.Request);
                            byte[] buf = Encoding.UTF8.GetBytes(rstr);
                            ctx.Response.ContentLength64 = buf.Length;
                            ctx.Response.OutputStream.Write(buf, 0, buf.Length);


                        }, _listener.GetContext());
                    }


            });
        }

        public void Stop()
        {
            _listener.Stop();
            _listener.Close();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

===========更新===========

  • 我将端口更改为8080
  • 代码中的所有try-catch-blocks被删除
  • 我确保端口不是问题:我测试过在端口8080上运行Tomcat servlet容器,并且它工作正常(当然,现在又停止了)
  • 端口8080未被任何其他应用程序或防火墙阻止

这是我启动应用程序时发生的情况:

netstat -na | 找到“ 8080”

C:\>netstat -na | find "8080"
  TCP    10.10.1.177:8080       0.0.0.0:0              LISTENING
  TCP    192.168.56.1:8080      0.0.0.0:0              LISTENING
  TCP    192.168.99.1:8080      0.0.0.0:0              LISTENING
Run Code Online (Sandbox Code Playgroud)

我不明白为什么在0.0.0.0:8080上什么也听不到(例如,当我在端口8080上运行Tomcat时就是这种情况),而是在其他IP上。

但是,我无法通过本地主机访问http服务器...

在此处输入图片说明

..或当我尝试通过这些IP地址访问时,返回“错误请求”响应,但没有

在此处输入图片说明

我还尝试了其他端口,例如7777或更高版本的10888。

ipconfig:

C:\>ipconfig

Windows-IP-Konfiguration

Ethernet-Adapter Ethernet:

   Verbindungsspezifisches DNS-Suffix: mycompany.ch
   Verbindungslokale IPv6-Adresse  . : fe80::5511:3eb5:408a:2562%4
   IPv4-Adresse  . . . . . . . . . . : 10.10.1.177
   Subnetzmaske  . . . . . . . . . . : 255.255.254.0
   Standardgateway . . . . . . . . . : 10.10.0.1

Drahtlos-LAN-Adapter WiFi:

   Medienstatus. . . . . . . . . . . : Medium getrennt
   Verbindungsspezifisches DNS-Suffix: mycompany.ch

Ethernet-Adapter VirtualBox Host-Only Network:

   Verbindungsspezifisches DNS-Suffix:
   Verbindungslokale IPv6-Adresse  . : fe80::b4b0:6fc5:d0fc:1c77%24
   IPv4-Adresse  . . . . . . . . . . : 192.168.56.1
   Subnetzmaske  . . . . . . . . . . : 255.255.255.0
   Standardgateway . . . . . . . . . :

Ethernet-Adapter VirtualBox Host-Only Network #2:

   Verbindungsspezifisches DNS-Suffix:
   Verbindungslokale IPv6-Adresse  . : fe80::b0e6:32c8:fc0a:af52%25
   IPv4-Adresse  . . . . . . . . . . : 192.168.99.1
   Subnetzmaske  . . . . . . . . . . : 255.255.255.0
   Standardgateway . . . . . . . . . :
Run Code Online (Sandbox Code Playgroud)

VirtualBox Adapters对该程序应该不是问题,对吗?

=============

只是为了进行比较-在我的PC的端口8080上运行Tomcat可以正常工作,就像这样:

C:\>netstat -na | find "8080"
  TCP    0.0.0.0:8080           0.0.0.0:0              LISTENING
  TCP    [::]:8080              [::]:0                 LISTENING
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

===========更新2 ===========

我将主机名从localhost更改为FQDN:

_listener.Prefixes.Add("http://devml-nb01-81.mycompany.ch:8080/test/");
Run Code Online (Sandbox Code Playgroud)

这样工作。

在此处输入图片说明

但是为什么它不能通过localhost(例如Tomcat?)工作呢?

C:\>ping localhost

Ping wird ausgeführt für devml-nb01-81.mycompany.ch [::1] mit 32 Bytes Daten:
Antwort von ::1: Zeit<1ms
Antwort von ::1: Zeit<1ms
Antwort von ::1: Zeit<1ms
Antwort von ::1: Zeit<1ms

Ping-Statistik für ::1:
    Pakete: Gesendet = 4, Empfangen = 4, Verloren = 0
    (0% Verlust),
Ca. Zeitangaben in Millisek.:
    Minimum = 0ms, Maximum = 0ms, Mittelwert = 0ms
Run Code Online (Sandbox Code Playgroud)

Ian*_*cer 3

打开提升的命令提示符并尝试

netsh http add urlacl url=http://+:8080/ user=EVERYONE
Run Code Online (Sandbox Code Playgroud)

(您需要使用 EVERYONE 的本地化名称或全局的SID 。)

使用 HTTP.sys 的应用程序需要预留。您可以在这里阅读更多相关内容。

顺便说一句,localhost这通常会在其他方面出现问题(例如,某些 OAuth 提供商不允许这样做)。解决这个问题的一种方法是使用已映射到 127.0.0.1 的 DNS 名称之一,例如 vcap.me。您可以在前面输入任何名称:foo.vcap.me它会解析为 127.0.0.1。