Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么HttpListener忽略外部请求?_C#_.net_Tcplistener_Httplistener - Fatal编程技术网

C# 为什么HttpListener忽略外部请求?

C# 为什么HttpListener忽略外部请求?,c#,.net,tcplistener,httplistener,C#,.net,Tcplistener,Httplistener,我的计算机在路由器后面,路由器将端口80转发给它(作为端口80)。我的问题是HttpListener似乎忽略了发往外部IP地址的请求 问题不在于路由器;如果我创建一个空白的C#console项目并只添加以下代码行: System.Net.Sockets.TcpListener s = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, 80); s.Start(); System.Net.Sockets.TcpClient c

我的计算机在路由器后面,路由器将端口80转发给它(作为端口80)。我的问题是HttpListener似乎忽略了发往外部IP地址的请求

问题不在于路由器;如果我创建一个空白的C#console项目并只添加以下代码行:

System.Net.Sockets.TcpListener s = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, 80);
s.Start();
System.Net.Sockets.TcpClient c = s.AcceptTcpClient();
System.Net.HttpListener s = new System.Net.HttpListener();
s.Prefixes.Add("http://+:80/testpath/");
s.Start();
System.Net.HttpListenerContext c = s.GetContext();
…并运行程序[注意:首次运行此程序时,用户可能必须允许防火墙例外],如果我导航到,它将终止

http://localhost/testpath/
http://externalipaddress/testpath/
http://localhost/testpath/
http://externalipaddress/testpath/
在我的浏览器中或如果我导航到

http://localhost/testpath/
http://externalipaddress/testpath/
http://localhost/testpath/
http://externalipaddress/testpath/
在我的浏览器中--任一地址都成功尝试端口80连接。因此,所有设置都是正确的——这台计算机可以在端口80上接受内部和外部连接

现在,如果我创建另一个空白的C#控制台项目并只添加以下代码行:

System.Net.Sockets.TcpListener s = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, 80);
s.Start();
System.Net.Sockets.TcpClient c = s.AcceptTcpClient();
System.Net.HttpListener s = new System.Net.HttpListener();
s.Prefixes.Add("http://+:80/testpath/");
s.Start();
System.Net.HttpListenerContext c = s.GetContext();
…首先,我会得到一个HttpListenerException,说由于中提到的问题,访问被拒绝。然后,当我关闭VisualStudio并以管理员身份重新打开它时,它将正常运行。当我导航到时,程序将(正确)终止

http://localhost/testpath/
http://externalipaddress/testpath/
http://localhost/testpath/
http://externalipaddress/testpath/
但是,如果导航到,我的请求就会超时

http://localhost/testpath/
http://externalipaddress/testpath/
http://localhost/testpath/
http://externalipaddress/testpath/

为什么我对HttpListener的外部请求超时?结果可以通过“ExternalPaddress”的数字IP地址或域名以及许多子文件夹、斜杠等的排列以及前缀“http://*:80/testpath/”、“http://externaldomain:80/testpath/”和“http://externaldomain:80/testpath/”重现“http://externalipddress:80/testpath/”(无空格;为stackoverflow格式添加).

事实证明Windows防火墙正在静默地接收来自本地主机外部的传入端口80请求。当我禁用防火墙时,HttpListener检测到外部请求。当我重新启用防火墙时,HttpListener停止检测外部请求。当我启用名为BranchCache Content Retrieval(HTTP In)”允许系统程序(承载为HttpListener执行TCP侦听的服务)在端口80上侦听,HttpListener再次检测到外部请求


要在Windows 7上查找入站规则列表,请启动->控制面板->系统和安全->Windows防火墙->高级设置->入站规则。

您是否询问过您的ISP是否阻止端口80上的入站连接?这很少见,但有些人会。还请尝试解析您的外部IP,使其指向
homeusr51363\u 516.your\u ISP.com
或类似如果我的ISP阻止了传入连接,那么第一个代码(通过外部IP地址连接到端口80)的成功与否需要解释吗?如前所述,我曾尝试在浏览器请求中使用数字IP地址和可解析域名。这是一个大问题。我不愿意为我使用的端口添加防火墙例外。我认为这是一个糟糕的做法。这方面还没有解决方案吗?根据防火墙的定义,必须为端口添加防火墙例外您想要使用的端口。这里的困难是HttpListener使用程序之外的东西在端口80上侦听,因此仅允许程序的防火墙异常不足以允许HttpListener侦听连接。谢谢Ben,您帮助我理解了这个问题。在我看来,这仍然是一个问题安全性问题,但由于没有其他解决方案,我将遵循您的建议。