API Fix closed response socket

This commit is contained in:
glax 2023-06-21 17:42:56 +02:00
parent abc66511d8
commit 6508055b43

View File

@ -67,8 +67,6 @@ public class Server
internal void SendResponse(HttpStatusCode statusCode, HttpListenerResponse response, object? content = null)
{
if (!response.OutputStream.CanWrite)
return;
//logger?.WriteLine(this.GetType().ToString(), $"Sending response: {statusCode}");
response.StatusCode = (int)statusCode;
response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With");
@ -76,9 +74,16 @@ public class Server
response.AddHeader("Access-Control-Max-Age", "1728000");
response.AppendHeader("Access-Control-Allow-Origin", "*");
response.ContentType = "application/json";
response.OutputStream.Write(content is not null
? Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(content))
: Array.Empty<byte>());
try
{
response.OutputStream.Write(content is not null
? Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(content))
: Array.Empty<byte>());
}
catch (HttpListenerException)
{
}
response.OutputStream.Close();
}