C# 400在C中使用简单Web服务器的错误请求(无效主机)#

C# 400在C中使用简单Web服务器的错误请求(无效主机)#,c#,webserver,nat,tcplistener,C#,Webserver,Nat,Tcplistener,我有一个小的C#控制台应用程序作为Web服务器。它在NAT上响应良好,设备位于同一网络中,但当我尝试从外部IP在浏览器中访问它时,我得到了400 路由器被配置为端口转发,否则我会得到404 localhost:8888/测试工作正常。 另外192.168.0.x:8888/对任何设备进行测试 xxx.xxx.xxx.xxx:8888/测试失败,HTTP错误为400。请求主机名无效 有什么建议吗 using System; using System.Collections.Generic; usi

我有一个小的C#控制台应用程序作为Web服务器。它在NAT上响应良好,设备位于同一网络中,但当我尝试从外部IP在浏览器中访问它时,我得到了400

路由器被配置为端口转发,否则我会得到404

localhost:8888/测试工作正常。 另外192.168.0.x:8888/对任何设备进行测试

xxx.xxx.xxx.xxx:8888/测试失败,HTTP错误为400。请求主机名无效

有什么建议吗

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);
            }
        }
    }
使用系统;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用系统文本;
使用System.Threading.Tasks;
名称空间httpsrv
{
班级计划
{
静态void Main(字符串[]参数)
{
Web服务器ws=新的Web服务器(SendResponse,“http://localhost:8888/test/");
ws.Run();
WriteLine(“Pi服务器已启动”);
Console.ReadKey();
ws.Stop();
}
公共静态字符串SendResponse(HttpListenerRequest请求)
{
返回string.Format(“Hosted from rasp.pi!
{0}”,DateTime.Now); } } }
Web服务器类:

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();
            }
        }
    }
使用系统;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用系统文本;
使用系统线程;
使用System.Threading.Tasks;
名称空间httpsrv
{
公共类Web服务器
{
私有只读HttpListener _listener=new HttpListener();
私有只读函数响应方法;
公共Web服务器(字符串[]前缀,Func方法)
{
如果(!HttpListener.IsSupported)
抛出新的NotSupportedException(
“需要Windows XP SP2、Server 2003或更高版本。”);
if(前缀==null | |前缀.长度==0)
抛出新的ArgumentException(“前缀”);
if(方法==null)
抛出新的ArgumentException(“方法”);
foreach(前缀中的字符串s)
_listener.Prefixes.Add(s);
_responderMethod=方法;
_listener.Start();
}
公共Web服务器(Func方法,参数字符串[]前缀)
:this(前缀、方法){}
公开募捐
{
ThreadPool.QueueUserWorkItem((o)=>
{
WriteLine(“正在运行的Web服务器…”);
尝试
{
while(_listener.IsListening)
{
ThreadPool.QueueUserWorkItem((c)=>
{
var ctx=c作为HttpListenerContext;
尝试
{
字符串rstr=\u responderMethod(ctx.Request);
字节[]buf=Encoding.UTF8.GetBytes(rstr);
ctx.Response.ContentLength64=基本长度;
ctx.Response.OutputStream.Write(buf,0,buf.Length);
}
捕获{}
最后
{
ctx.Response.OutputStream.Close();
}
},_listener.GetContext());
}
}
捕获{}
});
}
公共停车场()
{
_listener.Stop();
_listener.Close();
}
}
}
  • 您的DNS或名称解析错误
  • 没有将该流量转发到web服务器的路由
  • 检查端口转发您应该将端口8888转发到内部IP
  • 最后但并非最不重要的是检查防火墙,它应该允许端口8888
  • 看看你的代码,你似乎是在硬编码请求,把它变成一个变量,这样你就可以随时更改它

  • 我在ubuntu上使用自托管OWIN和c#时遇到了这个问题。 我通过将.exe中设置的基址设置为

    http://*:80 
    
    而不是

    http://192.168.1.1:80
    

    与@sean bradley有类似的问题-但在.net上

    这非常有效:

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

    加上使用“以管理员身份运行”模式启动应用程序(或命令提示符/Visual Studio),效果非常好

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