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

4

5

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();
            }
        }
    }
Bonnell answered 4/1, 2015 at 21:49 Comment(0)
C
1
  1. Either your DNS or name resolution is bad.
  2. There are no routes to forward that traffic to your web server
  3. Check your port forwarding you should be forwarding port 8888 to the internal IP
  4. Last but not least check your firewall, it should allow port 8888
  5. Looking at your code, it seems you are hard coding the request, make that a variable so that you can change it on the fly
Canicula answered 4/1, 2015 at 22:1 Comment(0)
M
8

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
Microfiche answered 18/3, 2016 at 11:11 Comment(0)
P
3

This

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

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

Pericycle answered 19/11, 2016 at 18:7 Comment(1)
'Run as administrator' work for me thank you baby. :)Chyack
C
1
  1. Either your DNS or name resolution is bad.
  2. There are no routes to forward that traffic to your web server
  3. Check your port forwarding you should be forwarding port 8888 to the internal IP
  4. Last but not least check your firewall, it should allow port 8888
  5. Looking at your code, it seems you are hard coding the request, make that a variable so that you can change it on the fly
Canicula answered 4/1, 2015 at 22:1 Comment(0)
E
0

Had similar issue as @sean-bradley - but on .net.

This worked great:

WebServer ws = new WebServer(SendResponse, "http://+:80/");
Extravagant answered 29/10, 2016 at 20:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.