HttpListener即使在允许防火墙的情况下也无法通过网络工作

问题描述 投票:1回答:1

我正在使用以下代码添加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,并以管理员权限启动了应用程序,但没有成功。

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

c# networking webserver firewall httplistener
1个回答
0
投票

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

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

    Start Visual Studio

  2. 以调试模式启动应用程序。

  3. 在Windows中打开资源监视器,并确保端口存在并将其设置为允许,不限制

    Resource monitor

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

    enter image description here

    允许在所有域上使用它:

    enter image description here

    允许程序:

    enter image description here

    允许连接:

    enter image description here

  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地址:

    enter image description here

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

    enter image description here

  3. 从另一台计算机,也进行测试以将HTTP请求发送到您的计算机:

    enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.