Ejemplo n.º 1
0
        private void add()
        {
            Product pro = new Product();

            Console.WriteLine("********* ADD ********");

            /*Console.Write("Enter Product ID: ");
             * pro.ProductID = Int16.Parse(Console.ReadLine());
             * string newURI = uri + "get-product/" + pro.ProductID.ToString();
             * if (client.GetAsync(newURI).Result.StatusCode == System.Net.HttpStatusCode.OK) {
             *  Console.WriteLine("ID existed!!!");
             *
             *  return;
             * }*/

            Console.Write("Enter Product Name: ");
            pro.ProductName = Console.ReadLine();

            Console.Write("Enter Product Price: ");
            pro.UnitPrice = Int16.Parse(Console.ReadLine());

            Console.Write("Enter Product Quantity: ");
            pro.Quantity = Int16.Parse(Console.ReadLine());

            #region get-public-key
            string newURI = baseURI + "get-public-key";
            //HttpResponseMessage response = await client.GetAsync(newURI);
            var response = client.GetStringAsync(newURI).Result.ToString();
            Obj obj      = JsonConvert.DeserializeObject <Obj>(response);
            PgpEncryptionKeys encryptionKeys = new PgpEncryptionKeys(obj.file);
            #endregion

            #region add API
            newURI = uri + "create-product/";

            PgpEncrypt encrypter = new PgpEncrypt(encryptionKeys);
            string     s         = JsonConvert.SerializeObject(pro);

            System.IO.MemoryStream output = new System.IO.MemoryStream();
            encrypter.EncryptAndSign(output, s);
            output.Close();

            KeyObj newObj = new KeyObj();
            newObj.kByte = output.ToArray();

            HttpResponseMessage resp = client.PostAsJsonAsync(newURI, newObj).Result;
            #endregion

            if (resp.StatusCode == System.Net.HttpStatusCode.OK)
            {
                Console.WriteLine("Add successful!!!");
            }
            else
            {
                Console.WriteLine("Add fail!!!");
            }
        }
Ejemplo n.º 2
0
        private void update()
        {
            Product pro = new Product();

            Console.WriteLine("********** UPDATE *********");
            Console.Write("Enter Product ID: ");
            pro.ProductID = Int16.Parse(Console.ReadLine());
            string newURI = uri + "get-product/" + pro.ProductID.ToString();

            //Console.WriteLine(newURI);
            if (client.GetAsync(newURI).Result.StatusCode == System.Net.HttpStatusCode.NotFound)
            {
                Console.WriteLine("ID not found!!");
                return;
            }


            Console.Write("Enter New Product Name: ");
            pro.ProductName = Console.ReadLine();
            Console.Write("Enter New Product Price: ");
            pro.UnitPrice = Int16.Parse(Console.ReadLine());
            Console.Write("Enter New Product Quantity: ");
            pro.Quantity = Int16.Parse(Console.ReadLine());


            #region get-public-key
            newURI = baseURI + "get-public-key";
            //HttpResponseMessage response = await client.GetAsync(newURI);
            var response = client.GetStringAsync(newURI).Result.ToString();
            Obj obj      = JsonConvert.DeserializeObject <Obj>(response);
            PgpEncryptionKeys encryptionKeys = new PgpEncryptionKeys(obj.file);
            #endregion

            #region update API
            newURI = uri + "update-product/" + pro.ProductID.ToString();

            PgpEncrypt encrypter = new PgpEncrypt(encryptionKeys);
            string     s         = JsonConvert.SerializeObject(pro);

            System.IO.MemoryStream output = new System.IO.MemoryStream();
            encrypter.EncryptAndSign(output, s);
            output.Close();

            //MemoryStream ms = new MemoryStream();
            //PGPHandler.GetInstance().EncryptAndSign(Encoding.UTF8.GetBytes(request), ms);
            //String encryptedData = Encoding.UTF8.GetString(ms.ToArray());

            KeyObj newObj = new KeyObj();
            newObj.kByte = output.ToArray();

            HttpResponseMessage resp = client.PutAsJsonAsync(newURI, newObj).Result;
            #endregion


            if (resp.StatusCode == System.Net.HttpStatusCode.OK)
            {
                Console.WriteLine("Update successful!!!");
            }
            else
            {
                Console.WriteLine("Update fail!!!");
            }
        }