簡體   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