【问题标题】:c# mono httplistener does not write http-headersc# mono httplistener 不写 http-headers
【发布时间】:2017-03-16 18:36:39
【问题描述】:

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

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

所以当向.net 中运行的服务器发送请求时,返回的响应是:

.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 独立应用程序,但我遇到了同样的问题,所以我认为单声道有问题。

【问题讨论】:

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


    【解决方案1】:

    显然,这是一个仍未修复的单声道错误 (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 响应,然后只是缺少标头。据我所知也没有解决方法,如果我错了,请纠正我。

    【讨论】:

      猜你喜欢
      • 2017-01-03
      • 1970-01-01
      • 2014-09-30
      • 2011-02-24
      • 2014-08-17
      • 2020-11-17
      • 1970-01-01
      • 1970-01-01
      • 2011-05-26
      相关资源
      最近更新 更多