c#mono httplistener不写http-headers

时间:2016-11-03 08:44:14

标签: c# .net mono http-headers httplistener

我有一个简单的网络服务器,我想在linux上使用mono运行。使用.net框架在Windows上一切正常,但是当我尝试以单声道运行相同的应用程序时,它的行为并不像预期的那样。

我正在使用mono 4.6.1.5 x64.net 4.5.2 x64

因此,当向.net中运行的服务器发送请求时,返回的响应为:

达网络

标题

Request URL:http://localhost:4040/
Request Method:GET
Status Code:200 OK
Remote Address:[::1]:4040
Response Headers
view source
Content-Length:9
Content-Type:text/html
Date:Thu, 03 Nov 2016 08:23:16 GMT
Server:Microsoft-HTTPAPI/2.0

预览

test data

然后,当我使用mono运行相同的构建时,服务器日志显示响应在浏览器加载10秒后立即完成,然后显示响应。现在的回应是:

标题

Request URL:http://localhost:4040/
Request Method:GET
Status Code:200 OK
Remote Address:127.0.0.1:4040

预览

P/1.1 200 OK
Content-Type: text/html
Server: Mono-HTTPAPI/1.0
Date: Thu, 03 Nov 2016 08:25:10 GMT
Content-Length: 9
Keep-Alive: timeout=15,max=100

test data

主侦听器循环

HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://*:4040/");
listener.Start();
while (true)
{
    HttpListenerContext context = listener.GetContext();
    Console.WriteLine ("Proccessing {0}", context.Request.Url.AbsolutePath);
    Process (context);
    Console.WriteLine ("Proccessing complete");
}

处理代码

private void Process(HttpListenerContext context)
{
    string data = "test data";
    WriteResponse(context.Response, data, HttpStatusCode.OK);
}

private void WriteResponse (HttpListenerResponse response, string data, HttpStatusCode status)
{
    response.StatusCode = (int)status;

    if (!string.IsNullOrEmpty(data))
    {
        response.ContentType = "text/html";
        response.ContentLength64 = data.Length;

        UTF8Encoding encoding = new UTF8Encoding();
        byte[] buffer = encoding.GetBytes(data);
        response.OutputStream.Write(buffer, 0, data.Length);
        response.OutputStream.Flush();
    }

    response.Close ();
}

我尝试使用nancy独立应用程序,我遇到了同样的问题,因此我认为单声道中存在错误。

1 个答案:

答案 0 :(得分:0)

显然它是单声道中仍然没有修复的错误(bugzilla)。如预览示例中所示:

P/1.1 200 OK
Content-Type: text/html
Server: Mono-HTTPAPI/1.0
Date: Thu, 03 Nov 2016 08:25:10 GMT
Content-Length: 9
Keep-Alive: timeout=15,max=100

test data

缺少http响应的前3个字符。

所以问题更多的是不正确的http响应,然后只是缺少标题。据我所知,也没有解决方法,如果我错了,请纠正我。