【问题标题】:HttpListener won't work over network even with firewall allowed即使允许防火墙,HttpListener 也无法通过网络工作
【发布时间】:2018-11-04 03:12:39
【问题描述】:

我正在使用下面的代码来添加一个 http 监听器:

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

    public WebServer(string[] prefixes, Func<HttpListenerRequest, HttpListenerResponse, string> method)
    {
        if (!HttpListener.IsSupported)
            throw new NotSupportedException(
                "Needs Windows XP SP2, Server 2003 or later.");

        // URI prefixes are required, for example 
        // "http://localhost:8080/index/".
        if (prefixes == null || prefixes.Length == 0)
            throw new ArgumentException("prefixes");

        // A responder method is required
        if (method == null)
            throw new ArgumentException("method");

        foreach (string s in prefixes)
            _listener.Prefixes.Add(s);

        _responderMethod = method;
        _listener.Start();
    }

    public WebServer(Func<HttpListenerRequest, HttpListenerResponse, string> method, params string[] prefixes)
        : this(prefixes, method) { }

    public void Run()
    {
        ThreadPool.QueueUserWorkItem((o) =>
        {
            //Console.WriteLine("Webserver running...");
            try
            {
                while (_listener.IsListening)
                {
                    ThreadPool.QueueUserWorkItem((c) =>
                    {
                        var ctx = c as HttpListenerContext;
                        try
                        {
                            string rstr = _responderMethod(ctx.Request, ctx.Response);
                            byte[] buf = Encoding.UTF8.GetBytes(rstr);
                            ctx.Response.ContentLength64 = buf.Length;
                            ctx.Response.OutputStream.Write(buf, 0, buf.Length);
                        }
                        catch { } // suppress any exceptions
                        finally
                        {
                            // always close the stream
                            ctx.Response.OutputStream.Close();
                        }
                    }, _listener.GetContext());
                }
            }
            catch { } // suppress any exceptions
        });
    }

    public void Stop()
    {
        _listener.Stop();
        _listener.Close();
    }
}

static class Program
{
    public const int PORT = 18991;

    [STAThread]
    static void Main()
    {
        WebServer ws = new WebServer(SendResponse, "http://+:" + PORT + "/");
        ws.Run();
        Application.Run(new Form1());
        ws?.Stop();
    }
    private static string SendResponse(HttpListenerRequest request, HttpListenerResponse response)
    {
        return "ok";
    }
}

这在本地机器上运行良好,但是它不会监听来自网络内其他设备的请求。我什至向防火墙添加了 outgoing 传入规则以允许连接,我使用netsh http add urlacl url="http://+:18991/" user=everyone 添加了 URL,并以管理员权限启动了应用程序,但没有成功。

如何允许局域网内远程设备的请求?

【问题讨论】:

  • I even added a outgoing rule to the firewall。你不认为它应该是一个传入规则...
  • @Eser 对不起,我的错误。我的意思是,我向防火墙添加了 incoming 规则。
  • however it won't listen to request from other devices inside the network. 你得到什么错误(在服务器端和客户端)?
  • @Eser 没有错误,只是请求设备超时,应用程序没有错误。
  • 你能ping通服务器吗?您可以连接到服务器端口(例如使用 telnet)吗? en.wikipedia.org/wiki/CURL

标签: c# networking webserver firewall httplistener


【解决方案1】:

您可以尝试以下步骤,看看它们是否有帮助:

  1. 以管理员身份启动 Visual Studio:

  2. 在调试模式下启动您的应用程序。

  3. 在 Windows 中打开 Resource Monitor 并确保端口存在并设置为 Allowed, not restricted

  4. 如果没有,为端口添加入站防火墙规则:

    在所有域上允许它:

    允许程序:

    允许连接:

  5. 按以下方式启动HttpListener

using System.IO;
using System.Net;
using System.Threading.Tasks;

namespace HttpTestServer
{
    public class MainWindow
    {
        private readonly HttpListener _httpListener;

        public MainWindow()
        {
            _httpListener = new HttpListener();
            _httpListener.Prefixes.Add("http://*:9191/");
            Task.Run(Start);
        }

        public async Task Start()
        {
            _httpListener.Start();
            while (true)
            {
                var context = await _httpListener.GetContextAsync();

                using (var sw = new StreamWriter(context.Response.OutputStream))
                {
                    await sw.FlushAsync();
                }
            }
        }
    }
}
  1. 运行 ipconfig 并检查您拥有的 IP 地址:

  2. 从网络上的另一台计算机 Ping 您的计算机:

  3. 从另一台计算机,也测试向您的计算机发送 HTTP 请求:

【讨论】:

    猜你喜欢
    • 2012-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    相关资源
    最近更新 更多