HttpListener 问题:每个 HTTP 请求都会导致 HttpListener 返回两个上下文

发布于 2024-12-10 10:15:49 字数 1480 浏览 0 评论 0原文

我已经在 Windows 服务应用程序中组装了一个小型嵌入式 HTTP 服务器,该服务器侦听来自网络上使用 HTTP 的其他设备的更新。

对于每个 HTTP 请求,处理请求/响应的代码都会执行两次,我希望它只运行一次。我尝试使用 AsyncGetContext 方法并使用同步版本 GetContext 来编写代码 - 最终结果是一样的。

代码

public void RunService()
{
    var prefix = "http://*:4333/";
    HttpListener listener = new HttpListener();
    listener.Prefixes.Add(prefix);
    try
    {
        listener.Start();
        _logger.Debug(String.Format("Listening on http.sys prefix: {0}", prefix));
    }
    catch (HttpListenerException hlex)
    {
        _logger.Error(String.Format("HttpListener failed to start listening. Error Code: {0}", hlex.ErrorCode));
        return;
    }
    while (listener.IsListening)
    {
        var context = listener.GetContext(); // This line returns a second time through the while loop for each request
        ProcessRequest(context);
    }
    listener.Close();
}

private void ProcessRequest(HttpListenerContext context) 
{
    // Get the data from the HTTP stream
    var body = new StreamReader(context.Request.InputStream).ReadToEnd();
    _logger.Debug(body);

    byte[] b = Encoding.UTF8.GetBytes("OK");
    context.Response.StatusCode = 200;
    context.Response.KeepAlive = false;
    context.Response.ContentLength64 = b.Length;

    var output = context.Response.OutputStream;
    output.Write(b, 0, b.Length);
    output.Close();
    context.Response.Close();
}

是否有任何明显的我遗漏的东西,我已经没有办法追踪问题了。

I have been putting together a little embedded HTTP server in a windows service app that listens for updates coming from other devices on the network that speak HTTP.

For each HTTP request, the code that processes the request/response is executed twice, I expect it to run only once. I tried the code using the AsyncGetContext method and using the synchronous version GetContext - the end result is the same.

Code

public void RunService()
{
    var prefix = "http://*:4333/";
    HttpListener listener = new HttpListener();
    listener.Prefixes.Add(prefix);
    try
    {
        listener.Start();
        _logger.Debug(String.Format("Listening on http.sys prefix: {0}", prefix));
    }
    catch (HttpListenerException hlex)
    {
        _logger.Error(String.Format("HttpListener failed to start listening. Error Code: {0}", hlex.ErrorCode));
        return;
    }
    while (listener.IsListening)
    {
        var context = listener.GetContext(); // This line returns a second time through the while loop for each request
        ProcessRequest(context);
    }
    listener.Close();
}

private void ProcessRequest(HttpListenerContext context) 
{
    // Get the data from the HTTP stream
    var body = new StreamReader(context.Request.InputStream).ReadToEnd();
    _logger.Debug(body);

    byte[] b = Encoding.UTF8.GetBytes("OK");
    context.Response.StatusCode = 200;
    context.Response.KeepAlive = false;
    context.Response.ContentLength64 = b.Length;

    var output = context.Response.OutputStream;
    output.Write(b, 0, b.Length);
    output.Close();
    context.Response.Close();
}

Is there anything obvious that I am missing, I have run out of ideas to track down the issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪之魂 2024-12-17 10:15:49

好的,问题是我使用 Web 浏览器来测试 HTTP 连接,默认情况下 Web 浏览器还会发送对 favicon.ico 的请求。所以实际上遇到了两个请求。感谢 @Inuyasha 建议我使用 Wireshark 检查一下。

Ok, the issue was I was using a web browser to test the HTTP connection and by default a web browser also sends a request for favicon.ico. So two requests were actually coming across. Thank you to @Inuyasha for suggesting I check things out with Wireshark.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文