コード例 #1
0
        private Member ReadRequstBody(HttpListenerContext context)
        {
            string body;
            var    jss = new JavaScriptSerializer();

            Console.WriteLine("starting reading the request stream");

            // Get the data from the HTTP stream
            using (var reader = new StreamReader(context.Request.InputStream))
            {
                body = reader.ReadToEnd();
                Console.WriteLine(body);

                byte[] b = Encoding.UTF8.GetBytes("ACK");
                context.Response.StatusCode      = 200;
                context.Response.KeepAlive       = false;
                context.Response.ContentLength64 = b.Length;

                var output = context.Response.OutputStream;
                output.Write(b, 0, b.Length);
                context.Response.Close();
            }

            Console.WriteLine("finished reading the request stream");
            return(JSONableExtensions.FromJson <Member>(body));
        }
コード例 #2
0
            internal void Serve(HttpListenerContext context, string resourcesPath)
            {
                Console.WriteLine("starting serving " + context.Request.HttpMethod + " " + context.Request.RawUrl + " on thread " + Thread.CurrentThread.ManagedThreadId);
                string body = readRequstBody(context.Request);

                Console.WriteLine(body);

                _staticData = new Member[] { };
                Dictionary <string, object> dict = new JavaScriptSerializer().Deserialize <Dictionary <string, object> >(body);


                Thread.Sleep(10000);

                using (StreamReader reader = new StreamReader(resourcesPath, Encoding.Unicode))
                {
                    Console.WriteLine("starting reading from the static file " + resourcesPath);
                    try
                    {
                        _staticData = JSONableExtensions.FromJsonArray <Member>(reader.ReadToEnd());
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    finally
                    {
                        byte[] b = Encoding.UTF8.GetBytes("ACK");
                        context.Response.StatusCode      = 200;
                        context.Response.KeepAlive       = false;
                        context.Response.ContentLength64 = b.Length;

                        var output = context.Response.OutputStream;
                        output.Write(b, 0, b.Length);
                        context.Response.Close();

                        Console.WriteLine("finished reading from the static file " + resourcesPath);
                        DataProcessing(dict);
                    }
                }
                Console.WriteLine("finished serving " + context.Request.RawUrl + " on thread " + Thread.CurrentThread.ManagedThreadId);
            }
コード例 #3
0
        internal void ServeStatic(HttpListenerContext context)
        {
            Console.WriteLine("starting serving... " + context.Request.HttpMethod + " " + context.Request.RawUrl + " on thread " + Thread.CurrentThread.ManagedThreadId);
            _staticData = new Member[] {};
            _requstBody = ReadRequstBody(context);

            //Thread.Sleep(10000);

            Console.WriteLine("finished to serving... " + context.Request.RawUrl + " on thread " + Thread.CurrentThread.ManagedThreadId);

            if (File.Exists(_resourcesPath))
            {
                var json = new JavaScriptSerializer();

                using (StreamReader file = File.OpenText(_resourcesPath))
                {
                    Console.WriteLine("starting reading from the static file " + _resourcesPath);
                    try
                    {
                        string input = file.ReadToEnd();
                        _staticData = JSONableExtensions.FromJsonArray <Member>(input);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    finally
                    {
                        Console.WriteLine("finished reading from the static file " + _resourcesPath);
                        DataProcessing(_requstBody, _staticData);
                    }
                }
            }
            else
            {
                Console.WriteLine("Static data file doesn't exist");
                context.Response.StatusCode = 404;
                context.Response.Close();
            }
        }