using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Web; namespace ConsoleApplication1 { class Program { // To enable this so that it can be run in a non-administrator account: // Open an Administrator command prompt. // netsh http add urlacl http://+:8008/ user=everyone const string Prefix = "http://+:8008/"; //The Url and port of the listening server that Commzgate will call. static HttpListener Listener = null; static void Main(string[] args) { if (!HttpListener.IsSupported) //Check if its working. { Console.WriteLine("HttpListener is not supported on this platform."); return; } using (Listener = new HttpListener()) { Listener.Prefixes.Add(Prefix); Listener.Start(); // Begin waiting for requests. Listener.BeginGetContext(GetContextCallback, null); //Press Enter to terminate program. Serves as a good way to keep the listener working. Console.WriteLine("Listening. Press Enter to stop process."); Console.ReadLine(); Listener.Stop(); } } //We implement a Callback, so that we can process multi-threaded request from Commzgate. static void GetContextCallback(IAsyncResult ar) { // Get the context var context = Listener.EndGetContext(ar); // listen for the next request Listener.BeginGetContext(GetContextCallback, null); // get the request String requestContent = ""; if (context.Request.HasEntityBody) { using (System.IO.Stream body = context.Request.InputStream) { using (System.IO.StreamReader reader = new System.IO.StreamReader(body, context.Request.ContentEncoding)) { requestContent = reader.ReadToEnd(); } } } Console.WriteLine("RAW request: "+ requestContent); // Parse the query string variables into a NameValueCollection. * Make sure you have the correct References included. NameValueCollection objectKeyParams = HttpUtility.ParseQueryString(requestContent); //Expected Values from Commzgate String mobilePhoneNumber = objectKeyParams.Get("Mobile"); String type = objectKeyParams.Get("Type"); String message = objectKeyParams.Get("Message"); String timestamp = objectKeyParams.Get("Timestamp"); String serviceNumber = objectKeyParams.Get("ServiceNum"); String connectionid = objectKeyParams.Get("ConnID"); String operatorID = objectKeyParams.Get("OperatorID"); //Do what you want here, but for now, we output to console instead. Console.WriteLine("Server sent " + objectKeyParams.Count + " parameters"); Console.WriteLine("Mobile:" + mobilePhoneNumber); Console.WriteLine("Type:" + type); Console.WriteLine("Message:" + message); Console.WriteLine("Timestamp:" + timestamp); Console.WriteLine("Service Number:" + serviceNumber); Console.WriteLine("Connection ID:" + connectionid); Console.WriteLine("Operator ID:" + operatorID); // format response: Commzgate Require OK string responseString = string.Format("OK"); byte[] buffer = Encoding.UTF8.GetBytes(responseString); // and send it var response = context.Response; response.ContentType = "text/plain"; response.ContentLength64 = buffer.Length; response.StatusCode = 200; response.OutputStream.Write(buffer, 0, buffer.Length); response.OutputStream.Close(); } } }