Skip to content

Instantly share code, notes, and snippets.

@katopz
Created December 23, 2015 07:30
Show Gist options
  • Save katopz/d882df3bb2df58d44108 to your computer and use it in GitHub Desktop.
Save katopz/d882df3bb2df58d44108 to your computer and use it in GitHub Desktop.
POC : HttpListener
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Diagnostics;
namespace TR.WebRTC.Services
{
public class MiniServer
{
public MiniServer()
{
if (!HttpListener.IsSupported)
{
Debug.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
return;
}
// URI prefixes are required,
string prefix = "http://localhost:3001/index/";
// Create a listener.
HttpListener listener = new HttpListener();
listener.Prefixes.Add(prefix);
listener.Start();
Debug.WriteLine("Listening...");
// Note: The GetContext method blocks while waiting for a request.
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
// 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();
listener.Stop();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment