c# mono httplistener 不写 http-headers
c# mono httplistener does not write http-headers
我有一个简单的网络服务器,我想 运行 在 linux 上使用单声道。使用 .net 框架在 windows 上一切正常,但是当我尝试 运行 单声道中的相同应用程序时,它的行为不如预期。
我正在使用 mono 4.6.1.5 x64
和 .net 4.5.2 x64
因此,当在 .net 中向服务器 运行ning 发送请求时,返回的响应是:
.net
headers
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
然后,当我 运行 使用单声道构建相同的构建时,服务器日志显示响应立即完成,而浏览器加载 10 秒后才显示响应。现在的响应是:
单声道
headers
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 独立应用程序,但我遇到了同样的问题,所以我认为它在单声道中有问题。
显然这是单声道中的一个错误,目前仍未修复 (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 响应然后只是缺少 headers。据我所知也没有解决方法,如果我错了请纠正我。
我有一个简单的网络服务器,我想 运行 在 linux 上使用单声道。使用 .net 框架在 windows 上一切正常,但是当我尝试 运行 单声道中的相同应用程序时,它的行为不如预期。
我正在使用 mono 4.6.1.5 x64
和 .net 4.5.2 x64
因此,当在 .net 中向服务器 运行ning 发送请求时,返回的响应是:
.net
headers
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
然后,当我 运行 使用单声道构建相同的构建时,服务器日志显示响应立即完成,而浏览器加载 10 秒后才显示响应。现在的响应是:
单声道
headers
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 独立应用程序,但我遇到了同样的问题,所以我认为它在单声道中有问题。
显然这是单声道中的一个错误,目前仍未修复 (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 响应然后只是缺少 headers。据我所知也没有解决方法,如果我错了请纠正我。