Example #1
0
        public static ResponsePackage ParseJson(String str)
        {
            ResponsePackage rsp = new ResponsePackage();
            Object          o   = Nskd.JsonV2.Parse(str);

            if ((o != null) && (o.GetType() == typeof(Hashtable)))
            {
                Hashtable package = (Hashtable)o;
                if ((package.ContainsKey("Status")) && (package["Status"] != null))
                {
                    rsp.Status = (String)package["Status"];
                }
                if (package.ContainsKey("Data") && (package["Data"] != null))
                {
                    rsp.Data = new DataSet();
                    Hashtable data = (Hashtable)package["Data"];
                    if (data.ContainsKey("tables") && (data["tables"] != null))
                    {
                        Object[] tables = (Object[])data["tables"];
                        for (int ti = 0; ti < tables.Length; ti++)
                        {
                            rsp.Data.Tables.Add((DataTable)tables[ti]);
                        }
                    }
                }
            }
            return(rsp);
        }
Example #2
0
        public static ResponsePackage ParseXml(String str)
        {
            ResponsePackage rsp = null;
            Encoding        enc = (str.Contains("utf-8")) ? Encoding.UTF8 : Encoding.Unicode;

            using (MemoryStream ms = new MemoryStream(enc.GetBytes(str)))
            {
                using (TextReader tr = new StreamReader(ms, enc))
                {
                    XmlSerializer sr = new XmlSerializer(typeof(ResponsePackage));
                    rsp = (ResponsePackage)sr.Deserialize(tr);
                }
            }
            return(rsp);
        }
Example #3
0
        public ResponsePackage GetResponse(String uri)
        {
            ResponsePackage rsp = new ResponsePackage();

            try
            {
                WebRequest wrq = WebRequest.Create(uri);
                wrq.Method = "POST";
                Byte[] buff = this.ToXml(Encoding.UTF8);
                //Byte[] buff = this.ToJson(Encoding.UTF8);
                Stream s = wrq.GetRequestStream();
                s.Write(buff, 0, buff.Length);
                s.Close();

                WebResponse  wrs  = wrq.GetResponse();
                StreamReader sr   = new StreamReader(wrs.GetResponseStream(), Encoding.UTF8);
                String       body = sr.ReadToEnd();
                sr.Close();

                if (!String.IsNullOrWhiteSpace(body))
                {
                    switch (body[0])
                    {
                    case '{':
                        rsp = ResponsePackage.ParseJson(body);
                        break;

                    case '<':
                        rsp = ResponsePackage.ParseXml(body);
                        break;

                    default:
                        rsp.Status = String.Format("Nskd.RequestPackage.GetResponse(): Response body format error: '{0}'", body);
                        break;
                    }
                }
                else
                {
                    rsp.Status = "Nskd.RequestPackage.GetResponse(): Response body is null or white space.";
                }
            }
            catch (Exception e)
            {
                rsp.Status = String.Format("Nskd.RequestPackage.GetResponse(): {0}", e.Message);
            }
            return(rsp);
        }