以下是代码,我们希望从响应中发送数据。但问题是它并不总是发送数据。
代码
var rstr = _responderMethod(ctx.Request);
var buf = Encoding.UTF8.GetBytes(rstr);
ctx.Response.ContentLength64 = buf.Length;
ctx.Response.OutputStream.Write(buf, 0, buf.Length);
var context = _listener.GetContext();
var request = context.Request;
using (var reader = new StreamReader(request.InputStream,
request.ContentEncoding))
{
var text = reader.ReadToEnd();
Console.WriteLine("Messages ************");
Console.WriteLine(text);
AiMessagePublisher.SendMessages(text);
}
有时它不会来Console.WriteLine("Messages ************");
任何指针都将受到赞赏。
答案 0 :(得分:0)
我使用以下代码解决了这个问题:
var context = _listener.GetContext();
try
{
var request = context.Request;
using (var reader = new StreamReader(request.InputStream,
request.ContentEncoding))
{
var text = reader.ReadToEnd();
Console.WriteLine("Messages ************");
Console.WriteLine(text);
publisher.SendMessages(text);
}
}
catch(Exception ex)
{
}
finally
{
context.Request.InputStream.Close();
}