How can I send Json response by using httpListener class. I have tried some approach but got no luck. I am able to send html and also json as HTML. But not proper json data like webAPI. Here is my piece of code.
HttpListenerResponse response = context.Response;
// Construct a response.
string responseString = //"<HTML><BODY>"+
ObjToJsonConverter(status);//+ "</BODY></HTML>";
// Get a response stream and write the response to it.
byte[] buffer = new byte[] { };
response.ContentType = "Application/json";
buffer = Encoding.ASCII.GetBytes(responseString);
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
output.Close();
// listener.Stop();
}
}
private static string ObjToJsonConverter(Status obj)
{
return "{\"statusText\":\""+obj.StatusText+",\"statusType\":\""+obj.StatusType+"\"}";
}
which I have tried. I tried to convert my object into jsonstring (above code).But problem of this approach is, response is rendering in chrome but in IE it is downloaded as file. any help would be appreciated.