繁体   English   中英

C#HttpListener没有响应

[英]C# HttpListener not responding

我在使用HttpListener创建本地网络服务器时遇到一些问题。 当我使用localhost uri(或127.0.0.1)时,它可以正常工作并且很好地响应请求。

但是,当我添加一些虚构的域(例如“ whateverabc.com”)时,服务器不再响应请求,chrome正在显示ERR_NAME_NOT_RESOLVED错误。

我缺少什么? 谢谢!

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

        _listener.IgnoreWriteExceptions = true;
        _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 { } // suppress any exceptions
                        finally
                        {
                            ctx.Response.OutputStream.Close();
                        }
                    }, _listener.GetContext());
                }
            }
            catch { } // suppress any exceptions
        });
    }

    public void Stop()
    {
        _listener.Stop();
        _listener.Close();
    }
}

static void Main(string[] args)
    {
        WebServer ws = new WebServer(SendResponse, "http://whateverabc.com:54785/");
        ws.Run();
        Console.WriteLine("A simple webserver. Press a key to quit.");
        Console.ReadKey();
        ws.Stop(); 
    }

    public static string SendResponse(HttpListenerRequest request)
    {
        return string.Format("<HTML><BODY>My web page.<br>{0}</BODY></HTML>", DateTime.Now);
    }

对于正在发生的事情似乎存在根本的误解。 这是东西:

通过注册前缀,您只需告诉您的Web服务器处理以该前缀开头并转到给定端口的请求即可。

但是,当您使用Chrome浏览器(或其他工具)访问您的网站时,首先会向配置的DNS服务器发送DNS请求,以查找“ whateverabc.com”域指向哪个IP地址。 而且由于该地址根本不存在(您可以在https://www.whois.com/上检查),因此您的请求失败。 因此,您的网络服务器不会收到开始请求。

以这种方式考虑(或尝试一下):如果要在本地计算机上启动Web服务器,并使其监听以“ http://www.microsoft.com ”开头的请求,您是否真的希望您的电话当您键入Microsoft网站时,如何从Chrome浏览器访问本地Web服务器?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM