// How to respond to a client's HTTP POST // Obtain a response object. HttpListenerResponse response = context.Response; // Construct a response. string responseString = "<HTML><BODY> Hello world!</BODY></HTML>"; byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); // Get a response stream and write the response to it. response.ContentLength64 = buffer.Length; System.IO.Stream output = response.OutputStream; output.Write(buffer,0,buffer.Length); // You must close the output stream. output.Close();
public string GetResponseContent(HttpWebResponse response) { if (response == null) { throw new ArgumentNullException("response"); } Stream dataStream = null; StreamReader reader = null; string responseFromServer = null; try { // Get the stream containing content returned by the server. dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. reader = new StreamReader(dataStream); // Read the content. responseFromServer = reader.ReadToEnd(); // Cleanup the streams and the response. } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { if (reader != null) { reader.Close(); } if (dataStream != null) { dataStream.Close(); } response.Close(); } LastResponse = responseFromServer; return responseFromServer; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)