Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

400 Bad Request (Invalid Host) using simple webserver in C#

I've got a small C# console app to work as a webserver. It responds well on the NAT with devices in the same network, but when I try to access it in a browser from the external IP i get a 400.

The router is configured to port forward, otherwise I get a 404.

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

xxx.xxx.xxx.xxx:8888/test fails with HTTP Error 400. The request hostname is invalid.

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);
            }
        }
    }

Webserver 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();
            }
        }
    }
like image 914
OHMR Avatar asked Dec 26 '22 00:12

OHMR


2 Answers

I had this issue on ubuntu when using self hosted OWIN and c#. I fixed it by setting the base address set inside my .exe to

http://*:80 

instead of

http://192.168.1.1:80
like image 95
Sean Bradley Avatar answered Dec 28 '22 09:12

Sean Bradley


This

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

plus starting application (or command prompt / Visual Studio) with 'Run as administrator' mode worked great!

like image 35
Abc Avatar answered Dec 28 '22 11:12

Abc