编程语言
首页 > 编程语言> > C#http侦听器不在localhost上侦听,只能与FQDN一起使用

C#http侦听器不在localhost上侦听,只能与FQDN一起使用

作者:互联网

(请删除近距离投票.恕我直言,这是一个有效的问题,提供了所有必要的信息,但试图简要介绍确定问题可能来源所需的所有必要信息).

我是来自Java世界的C#的新手.我试着让一个简单的http服务器在localhost上侦听一个端口(在我的例子中是8080).

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

当我运行该程序时,它说

enter image description here

但是我无法通过浏览器到达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();  
}

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();
        }
    }
}

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

>我将端口更改为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

我不明白为什么没有什么东西在0.0.0.0:8080听(例如当我在端口8080上运行Tomcat时就是这种情况),但是在那些其他IP上.

但是,我无法通过localhost访问http服务器…

enter image description here

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

enter image description here

我还尝试过其他端口,如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 . . . . . . . . . :

VirtualBox适配器应该不是该程序的问题,对吧?

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

只是为了比较它 – 在我的电脑上运行端口8080上的Tomcat工作正常,就像这样:

C:\>netstat -na | find "8080"
  TCP    0.0.0.0:8080           0.0.0.0:0              LISTENING
  TCP    [::]:8080              [::]:0                 LISTENING

enter image description here

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

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

_listener.Prefixes.Add("http://devml-nb01-81.mycompany.ch:8080/test/");

这种方式有效.

enter image description here

但为什么它不能通过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

解决方法:

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

netsh http add urlacl url=http://+:8080/ user=EVERYONE

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

使用HTTP.sys的应用程序需要保留.您可以阅读有关该here的更多信息.

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

标签:c,http,httplistener
来源: https://codeday.me/bug/20190702/1354633.html