Example #1
0
        public bool addUser(USER_INFO info)
        {
            string cmd = "addUser";
            ErrorCode = ERR_CODE_UNKNOWN_RESPONSE;
            ErrorMsg = ERR_MSG_UNKNOWN_RESPONSE;    //default error message

            try
            {
                StringBuilder sbReq = new StringBuilder();

                sbReq.Append(String.Format("{0}API?command={1}&session_id={2}", httpUri.AbsoluteUri, cmd, SessionId));
                sbReq.Append(String.Format("&username={0}&password={1}&desc={2}&level={3}", info.username, info.password, info.desc, info.level));

                string resp = sendHTTPRequest(sbReq.ToString());
                if (resp == null) return false;
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(resp);
                if (isCSLResponse(ref doc, cmd) == false) return false;

                XmlNode node = doc.SelectSingleNode("CSL/Ack");
                if (node != null)
                {
                    string value = node.InnerXml;
                    if (value.StartsWith("OK", StringComparison.OrdinalIgnoreCase))
                    {
                        ErrorCode = ERR_CODE_NO_ERROR;
                        ErrorMsg = "";
                        return true;
                    }
                }
                parseErrorCode(ref doc);
                return false;
            }
            catch
            {
                ErrorCode = ERR_CODE_UNKNOWN_ERROR;
                ErrorMsg = ERR_MSG_UNKNOWN_ERROR;
                return false;
            }
        }
Example #2
0
        public System.Collections.ArrayList listUsers()
        {
            System.Collections.ArrayList list = new System.Collections.ArrayList();

            string cmd = "listUsers";
            ErrorCode = ERR_CODE_UNKNOWN_RESPONSE;
            ErrorMsg = ERR_MSG_UNKNOWN_RESPONSE;    //default error message

            try
            {
                StringBuilder sbReq = new StringBuilder();

                sbReq.Append(String.Format("{0}API?command={1}&session_id={2}", httpUri.AbsoluteUri, cmd, SessionId));

                string resp = sendHTTPRequest(sbReq.ToString());
                if (resp == null)
                    return null;

                XmlDataDocument doc = new XmlDataDocument();

                doc.LoadXml(resp);

                XmlElement element = doc.DocumentElement;
                if (element.Name == "CSL")
                {
                    XmlNode node = doc.SelectSingleNode("CSL/Command");
                    if (node != null)
                    {
                        if (node.InnerXml.Equals("listUsers", StringComparison.OrdinalIgnoreCase))
                        {
                            node = doc.SelectSingleNode("CSL/Account");
                            if (node != null)
                            {
                                while (node != null)
                                {
                                    USER_INFO info = new USER_INFO();

                                    XmlAttributeCollection atts = node.Attributes;
                                    info.username = atts.GetNamedItem("username").Value;
                                    //info.password = atts.GetNamedItem("password").Value;
                                    info.desc = atts.GetNamedItem("desc").Value;
                                    info.level = int.Parse(atts.GetNamedItem("level").Value);

                                    list.Add(info);
                                    node = node.NextSibling;
                                }
                                ErrorCode = ERR_CODE_NO_ERROR;
                                ErrorMsg = "";

                                return list;
                            }

                            node = doc.SelectSingleNode("CSL/Error");
                            if (node != null)
                            {
                                //<Error>
                                foreach (XmlAttribute att in node.Attributes)
                                {
                                    if (att.Name.Equals("msg", StringComparison.OrdinalIgnoreCase))
                                    {
                                        ErrorMsg = att.InnerXml.Substring(6).Trim();
                                    }
                                    else if (att.Name.Equals("code", StringComparison.OrdinalIgnoreCase))
                                    {
                                        Int32.TryParse(att.InnerXml, out ErrorCode);
                                    }
                                }
                                return null;
                            }
                        }
                    }
                }
            }
            catch (WebException wex)
            {
                ErrorMsg = wex.Message;
                return null;
            }
            ErrorMsg = ERR_MSG_UNKNOWN_RESPONSE;
            return null;
        }