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

标签 c# http httplistener

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

我是来自 Java 世界的 C# 新手。我尝试让一个简单的 http 服务器监听本地主机上的端口(在我的例子中是 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服务器程序时,该端口未被其他应用程序阻塞或使用。

代码:

程序.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 未被任何其他端口阻止 应用程序或防火墙

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

网络统计-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 上监听。

但是,我无法通过本地主机访问 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 适配器应该不是该程序的问题,对吧?

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

只是比较一下 - 在我的 PC 上的端口 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 监听器不在本地主机上监听,仅适用于 FQDN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36846535/

相关文章:

java - 将 int 值添加到 char 数组 ASCII 值 C# 和 JAVA

c# - 将文件复制到远程位置会间歇性地抛出找不到网络路径

http - 当包含用户名和密码时,客户端应如何处理 HTTP 响应中的 Location 字段?

http - oauth2.0 如何传递访问 token

c# - SSL 客户端证书/与 ServiceStack 的相互身份验证 (HttpListener)

c# - Visual Studio 默认打开文件操作

c# - 有没有办法以编程方式判断系统是否启用了触摸?

http - REST 的用例

c# - WebDavSharp 服务器。如何使用 Word 选项共享链接