using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;

namespace WpfApplication19
{
    class WebAppConnector
    {
        private WebRequest request;
        public string PostData { get; set; }
        public string Uri { get; set; }
        public string Method { get; set; }

        public Response sendRequest()
        {
            Response r = new Response();

            WebRequest request = WebRequest.Create(this.Uri);

            request.Method = this.Method;
            byte[] byteArray = Encoding.UTF8.GetBytes(this.PostData);
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteArray.Length;
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);

            dataStream.Close();
			
			WebResponse response = request.GetResponse();
			
            //Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            r.status = ((HttpWebResponse)response).StatusDescription.Trim();
            dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();
            //Console.WriteLine(responseFromServer);
            JObject o = JObject.Parse(responseFromServer);
            //Console.WriteLine(o["msg"]);
            r.jsonBody = o;
            reader.Close();
            dataStream.Close();
            response.Close();
            //Console.ReadLine();
            return r;
        }
    }
    class Response
    {
        public string status;
        public JObject jsonBody;

    }
}
