【发布时间】: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