Saturday 9 November 2013

CCS Server API (To Get, Post, Put & Delete the data on the server)

To access api you need to create get, post, put or delete requests so we can create requests like the following codes.

 <appSettings>
    <add key="CCSAPIURL" value="https://265.105.115.68:9303/" />
    <add key="CCSAPIUsername" value="admin" />
    <add key="CCSAPIPassword" value="admin" />
 </appSettings>

protected void lnkbtnSave_Click(object sender, EventArgs e)
        {
   ServerOperations serverapi = new ServerOperations();
            string LoginID = txtLoginID.Text;
            string Password = txtLoginPassword.Text;
            string Email = txtEmail.Text;
 
   string Data = "groupName=bluevoip.com&userName=" + LoginID + "&password=" + Password + "&profileName=mobile_sip";
            serverapi.PostUserDetails(Data, "ccs/user");
            string data = "userName=" + LoginID + "&attributeName=account.notification._userEmailAddress&attributeValue=" + Email;
            serverapi.PostUserDetails(data, "ccs/user/attribute");  
  }

public class ServerOperations
    {
        string URL = Convert.ToString(ConfigurationManager.AppSettings["CCSAPIURL"]);
        string UserName = Convert.ToString(ConfigurationManager.AppSettings["CCSAPIUsername"]);
        string Password = Convert.ToString(ConfigurationManager.AppSettings["CCSAPIPassword"]);
        /// <summary>
        /// For Secure server certify
        /// </summary>
        public static void InitiateSSLTrust()
        {
            try
            {
                //Change SSL checks so that all checks pass
                ServicePointManager.ServerCertificateValidationCallback =
                    new RemoteCertificateValidationCallback(
                        delegate
                        { return true; }
                    );
            }
            catch (Exception)
            {
            }
        }
        /// <summary>
        /// Get the User Details from the CCS
        /// </summary>
        public void GetUserDetails()
        {
            try
            {
                InitiateSSLTrust();               
                string url = "https://265.105.115.68:9303/ccs/user?userName=test@test.com";
                WebRequest myReq = WebRequest.Create(url);
                string username = "developer1";
                string password = "D3v3l0p3r";
                string usernamePassword = username + ":" + password;
                CredentialCache mycache = new CredentialCache();
                mycache.Add(new Uri(url), "Digest", new NetworkCredential(username, password));
                myReq.Credentials = mycache;
                myReq.Headers.Add("Authorization", "Digest " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));
                WebResponse wr = myReq.GetResponse();
                Stream receiveStream = wr.GetResponseStream();
                StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
                string content = reader.ReadToEnd();
                //txtResponse.Text = "Response : " + content;
            }
            catch (WebException ex)
            {
            }
        }
        /// <summary>
        /// Post Userdata to the server
        /// </summary>
        /// <param name="Data"></param>
        /// <param name="AppendURL"></param>
        public void PostUserDetails(string Data, string AppendURL)
        {
            try
            {              
                InitiateSSLTrust();
                string url = URL + AppendURL;
                string data = Data;
                WebRequest myReq = WebRequest.Create(url);
                myReq.Method = "POST";
                byte[] bytes = Encoding.UTF8.GetBytes(data);
                myReq.ContentLength = bytes.Length;
                myReq.ContentType = "application/x-www-form-urlencoded";
                string username = UserName;
                string password = Password;
                string usernamePassword = username + ":" + password;
                CredentialCache mycache = new CredentialCache();
                mycache.Add(new Uri(url), "Digest", new NetworkCredential(username, password));
                myReq.Credentials = mycache;
                myReq.Headers.Add("Authorization", "Digest " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));
                using (var requestStream = myReq.GetRequestStream())
                {
                    requestStream.Write(bytes, 0, bytes.Length);
                    requestStream.Close();
                }
                WebResponse wr = myReq.GetResponse();
                Stream receiveStream = wr.GetResponseStream();
                StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
                string content = reader.ReadToEnd();
            }
            catch (WebException ex)
            {               
            }
        }
        /// <summary>
        /// Modify Userdate on the server
        /// </summary>
        /// <param name="data"></param>
        /// <param name="AppendURL"></param>
        public void PUTUserDetails(string data, string AppendURL)
        {
            try
            {
                InitiateSSLTrust();
                string url = URL + AppendURL;
                WebRequest myReq = WebRequest.Create(url);               
                myReq.Method = "PUT";
                byte[] bytes = Encoding.UTF8.GetBytes(data);
                myReq.ContentLength = bytes.Length;
                myReq.ContentType = "application/x-www-form-urlencoded";
                string username = "developer1";
                string password = "D3v3l0p3r";
                string usernamePassword = username + ":" + password;
                CredentialCache mycache = new CredentialCache();
                mycache.Add(new Uri(url), "Digest", new NetworkCredential(username, password));
                myReq.Credentials = mycache;
                myReq.Headers.Add("Authorization", "Digest " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));
                using (var requestStream = myReq.GetRequestStream())
                {
                    requestStream.Write(bytes, 0, bytes.Length);
                    requestStream.Close();
                }
                WebResponse wr = myReq.GetResponse();
                Stream receiveStream = wr.GetResponseStream();
                StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
                string content = reader.ReadToEnd();               
            }
            catch (WebException ex)
            {               
            }
        }
        /// <summary>
        /// Delete the user from the server
        /// </summary>
        /// <param name="Data"></param>
        /// <param name="AppendURL"></param>
        public void DeleteUserDetails(string Data, string AppendURL)
        {
            try
            {
                InitiateSSLTrust();
                string url = URL + AppendURL;
                string data = Data;
                WebRequest myReq = WebRequest.Create(url);
                myReq.Method = "DELETE";
                byte[] bytes = Encoding.UTF8.GetBytes(data);
                myReq.ContentLength = bytes.Length;
                myReq.ContentType = "application/x-www-form-urlencoded";
                string username = UserName;
                string password = Password;
                string usernamePassword = username + ":" + password;
                CredentialCache mycache = new CredentialCache();
                mycache.Add(new Uri(url), "Digest", new NetworkCredential(username, password));
                myReq.Credentials = mycache;
                myReq.Headers.Add("Authorization", "Digest " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));
                using (var requestStream = myReq.GetRequestStream())
                {
                    requestStream.Write(bytes, 0, bytes.Length);
                    requestStream.Close();
                }
                WebResponse wr = myReq.GetResponse();
                Stream receiveStream = wr.GetResponseStream();
                StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
                string content = reader.ReadToEnd();
            }
            catch (WebException ex)
            {
            }
        }       
    }

No comments:

Post a Comment