Esempio n. 1
0
        void ProcessRequest(HttpListenerContext context)
        {
            string path = context.Request.Url.LocalPath;

            if (path == "/favicon.ico")
            {
                context.Response.Close();
                return;
            }
            try
            {
                Msg m = null;
                if (context.Request.HttpMethod == "POST")
                {
                    Type requestType = typeof(Msg);
                    m = (Msg)Serializer.NonGeneric.Deserialize(requestType, context.Request.InputStream);
                    if (m != null)
                    {
                        Serializer.NonGeneric.Serialize(context.Response.OutputStream, 200);
                        context.Response.Close();
                        store.Add(m);
                    }
                }
            }
            catch
            {
                Serializer.NonGeneric.Serialize(context.Response.OutputStream, 500);
                context.Response.Close();
            }
        }
Esempio n. 2
0
        private void ProcessRequest(IAsyncResult result)
        {
            // http://stackoverflow.com/questions/427326/httplistener-server-header-c-sharp

            HttpListener        listener = (HttpListener)result.AsyncState;
            HttpListenerContext context  = listener.EndGetContext(result);

            string method = context.Request.HttpMethod;
            string path   = context.Request.Url.LocalPath;

            switch (path)
            {
            case "/favicon.ico":
                context.Response.Close();
                return;

            case "/ping":
                byte[] buffer = Encoding.UTF8.GetBytes("OK");
                context.Response.ContentLength64 = buffer.Length;
                System.IO.Stream output = context.Response.OutputStream;
                output.Write(buffer, 0, buffer.Length);
                output.Close();
                break;

            default:
                if (method == "POST")
                {
                    try
                    {
                        Msg  m           = null;
                        Type requestType = typeof(Msg);
                        m = (Msg)Serializer.NonGeneric.Deserialize(requestType, context.Request.InputStream);
                        if (m != null)
                        {
                            Serializer.NonGeneric.Serialize(context.Response.OutputStream, 200);
                            context.Response.Close();
                            store.Add(m);
                        }
                    }
                    catch
                    {
                        Serializer.NonGeneric.Serialize(context.Response.OutputStream, 500);
                        context.Response.Close();
                    }
                }
                break;
            }

            listener.BeginGetContext(ProcessRequest, listener);
        }