代码之家  ›  专栏  ›  技术社区  ›  Robert Oschler Rob

使用SimpleHttpServer的C#/.NET应用程序不向浏览器返回响应?

  •  0
  • Robert Oschler Rob  · 技术社区  · 5 年前

    注意:这不是Python应用程序,它是C#。

    我已经创建了一个C#应用程序,我正试图使用我在网上找到的这个简单的C#模块将其制作成一个简单的服务器:

    https://gist.github.com/aksakalli/9191056

    网络 Chrome调试器中的选项卡。我看到请求被提出,但是响应子面板总是空的。

    我跟踪了我的服务器代码,它运行时没有出错,但是在将头添加到响应并关闭输出流之后,浏览器端什么也没有发生。下面是我代码的相关部分。有人能告诉我为什么我的浏览器从来没有看到响应,我怎么能解决这个问题吗?

        public delegate Task<string> ProcessHttpContext(HttpListenerContext context);
        private ProcessHttpContext _processHttpContextFunc = null;
    
        public SimpleHTTPServer(string path, int port, ProcessHttpContext processHttpContextFunc)
        {
            this.Initialize(path, port);
            this._processHttpContextFunc = processHttpContextFunc;
        }
    
        public SimpleHTTPServer(string path)
        {
            //get an empty port
            TcpListener l = new TcpListener(IPAddress.Loopback, 0);
            l.Start();
            int port = ((IPEndPoint)l.LocalEndpoint).Port;
            l.Stop();
            this.Initialize(path, port);
        }
    
        public void Stop()
        {
            _serverThread.Abort();
            _listener.Stop();
        }
    
        private void Listen()
        {
            _listener = new HttpListener();
            _listener.Prefixes.Add("http://+:" + _port.ToString() + "/main-page/");
            _listener.Start();
    
            while (true)
            {
                try
                {
                    HttpListenerContext context = _listener.GetContext();
                    Process(context);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Exception occurred during GetContext() call: " + ex.Message);
                    if (ex.InnerException != null)
                        Debug.WriteLine("  Inner Exception: " + ex.InnerException.Message);
                }
            }
        }
    
        async private void Process(HttpListenerContext context)
        {
            if (context == null)
                throw new ArgumentNullException("The context parameter is empty.");
    
            try
            {
                    // Call it with the current context.
                    string strResult = await this._processHttpContextFunc(context);
                    byte[] bytes = System.Text.Encoding.UTF8.GetBytes(strResult);
    
                    context.Response.AddHeader("Content-Type", "text/plain; charset=UTF-8");
                    context.Response.AddHeader("Content-Length", strResult.Length.ToString());
                    context.Response.SendChunked = false;
                    context.Response.StatusCode = (int)HttpStatusCode.OK;
                    context.Response.StatusDescription = "OK";
                    context.Response.OutputStream.Write(bytes, 0, bytes.Length);
                    context.Response.OutputStream.Flush();
                }
            }
            catch (Exception ex)
            {
                context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
            }
            context.Response.OutputStream.Close();
        }
    
        private void Initialize(string path, int port)
        {
            this._rootDirectory = path;
            this._port = port;
            _serverThread = new Thread(this.Listen);
            _serverThread.Start();
        }
    
    0 回复  |  直到 5 年前