【问题标题】:HttpListener not responding on public ipHttpListener 在公共 IP 上没有响应
【发布时间】:2021-04-29 10:35:30
【问题描述】:

我的 HttpListener 代码在接收 LAN 请求时运行良好,但是当我使用我的公共 ip 发送请求时,侦听器没有响应。我已经打开了我的监听器正在使用的端口(:8080),它与运行代码的计算机的 ip 相关联。 192.168.2.80。我使用端口检查器工具检查了端口是否打开,并且当代码运行时端口检查器确实发现端口处于活动状态。但是仍然没有收到来自我的公共 IP 的请求。我做错了什么?

我还禁用了我的路由器防火墙以及我的计算机。

        static void Main(string[] args)
        {
            var prefixes = new List<string>() { "http://*:8080/" };
            HttpListener server = new HttpListener();
            foreach (string s in prefixes)
            {
                server.Prefixes.Add(s);
            }

            try
            {
                server.Start();
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return;
            }

            while (true)
            {
                HttpListenerContext context = server.GetContext();
                HttpListenerRequest request = context.Request;
                HttpListenerResponse response = context.Response;
                string responseString;

                Stream stream = request.InputStream;
                StringBuilder stringBuilder = new StringBuilder();
                byte[] inputBuffer = new byte[request.ContentLength64];

                int dataRead = stream.Read(inputBuffer, 0, (int)request.ContentLength64);
                stringBuilder.Append(Encoding.ASCII.GetString(inputBuffer));
 var client = new RestClient("http://141.237.167.74:8080");
            var request = new RestRequest(Method.POST);
            request.Timeout = Timeout.Infinite;

            string data = "{'username':'MARKATOS','gameid':'bcfc775e-9215-4dcb-9116-f52b64a9b55d','move':'P1001'}";
            string json = "{'action':'UploadMove','data':\"" + data + "\"}";
            request.AddJsonBody(json);

            IRestResponse response = client.Execute(request);

【问题讨论】:

    标签: c# http port httplistener


    【解决方案1】:

    事实证明,HttpListener 确实在使用我的公共 IP,而不是来自运行代码的我自己的计算机。我本地网络之外的任何其他人都可以成功地向我发送 http 请求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 2020-07-27
      • 1970-01-01
      相关资源
      最近更新 更多