HTTP 侦听器未收到外部调用

时间:2021-02-04 15:14:03

标签: c# winforms httplistener

我有这个 Windows 窗体应用程序想要接收来自某个外部来源的一些呼叫。

我基于此做了一些示例:

https://docs.microsoft.com/en-us/dotnet/api/system.net.httplistener?view=net-5.0

几乎是一样的,我只是为了在另一个线程上运行。

    private static Form1 form;
    public static Thread httpServer;

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        form = new Form1();
        form.Load += OnFormLoad;
        Application.Run(form);
    }
    private static void OnFormLoad(object sender, EventArgs e)
    {
        httpServer = CreateHttp();
    }
    private static Thread CreateHttp()
    {
        HttpListener listener = new HttpListener();
        string[] prefixes = new string[1];

        prefixes[0] = "http://+:3070/";

        foreach (string s in prefixes)
        {
            listener.Prefixes.Add(s);
        }
        listener.Start();

        var listeningThread = new Thread(() =>
        {
            while (true)
            {
                HttpListenerContext context = listener.GetContext();
                ThreadPool.QueueUserWorkItem(param =>
                {

                    HttpListenerRequest request = context.Request;
                    HttpListenerResponse response = context.Response;

                    string url = request.RawUrl;
                    
                    string responseString = "<HTML><BODY> Hello World </BODY></HTML>";
                    byte[] buffer = Encoding.UTF8.GetBytes(responseString);

                    response.ContentLength64 = buffer.Length;
                    Stream output = response.OutputStream;
                    output.Write(buffer, 0, buffer.Length);

                    output.Close();

                }, null);
            }
        });

        listeningThread.IsBackground = true;
        listeningThread.Start();

        return listeningThread;
    }

当我从 localhost:3070 或 127.0.0.1:3070 调用时,它可以工作,但如果我从 ipadress:3070 尝试,我只会超时。

我的防火墙中已经包含了这个“3070”端口......我也尝试关闭整个防火墙

还检查侦听器是否正在使用命令:

netstat -na |找到“3070”

TCP 0.0.0.0:3070 0.0.0.0:0 监听 TCP [::]:3070 [::]:0 监听

并且还检查了这个 url 是否被命令保留:

"netsh http 显示 urlacl"

URL reservada            : http://+:3070/
    Usuário: \Todos
        Escutar: Yes
        Delegar: No
        SDDL: D:(A;;GX;;;WD)

有什么我遗漏的吗?

0 个答案:

没有答案
相关问题