400 Bad Request (Invalid Host) using a simple web server in C #

I have a small C # console application to work as a web server. It responds well to NAT with devices on the same network, but when I try to access it in a browser from an external IP, I get 400.

The router is configured to migrate, otherwise I get 404.

localhost: 8888 / test is working fine. also 192.168.0.x: 8888 / test for any device.

xxx.xxx.xxx.xxx:8888/test fails with HTTP 400 error. Invalid request host name.

Any suggestions?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace httpsrv
    {
    class Program
        {
        static void Main(string[] args)
            {
            WebServer ws = new WebServer(SendResponse, "http://localhost:8888/test/");
            ws.Run();
            Console.WriteLine("Pi server started");
            Console.ReadKey();
            ws.Stop();
            }

        public static string SendResponse(HttpListenerRequest request)
            {
            return string.Format("<HTML><BODY>Hosted from rasp. pi!<br>{0}</BODY></HTML>", DateTime.Now);
            }
        }
    }

Web server class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

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

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

            if (prefixes == null || prefixes.Length == 0)
                throw new ArgumentException("prefixes");

            if (method == null)
                throw new ArgumentException("method");

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

            _responderMethod = method;
            _listener.Start();
            }

        public WebServer(Func<HttpListenerRequest, 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);
                                byte[] buf = Encoding.UTF8.GetBytes(rstr);
                                ctx.Response.ContentLength64 = buf.Length;
                                ctx.Response.OutputStream.Write(buf, 0, buf.Length);
                                }
                            catch { } 
                            finally
                                {
                                ctx.Response.OutputStream.Close();
                                }
                        }, _listener.GetContext());
                       }
                    }
                catch { }
            });
            }

        public void Stop()
            {
            _listener.Stop();
            _listener.Close();
            }
        }
    }
+4
source share
4 answers
  • Is your DNS or name resolution bad?
  • -.
  • , 8888 IP
  • , , 8888
  • , , , ,
+1

ubuntu OWIN #. , , .exe,

http://*:80 

http://192.168.1.1:80
+7

WebServer ws = new WebServer(SendResponse, "http://*:80/");

( /Visual Studio) " " !

+1

, @sean-bradley - .net.

:

WebServer ws = new WebServer(SendResponse, "http://+:80/");
0

Source: https://habr.com/ru/post/1569922/


All Articles