public async Task PostData(SomeData data)
        {
            var json        = JsonConvert.SerializeObject(data);
            var requestData = new StringContent(json, Encoding.UTF8, "application/json");
            var url         = "http://localhost:5000/somedata";
            var response    = await m_Client.PostAsync(url, requestData);

            string result = response.Content.ReadAsStringAsync().Result;

            Console.WriteLine($"{(int)response.StatusCode} {response.StatusCode} {result}");
        }
        public async Task PutById(string id, SomeData data)
        {
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Put, $"http://localhost:5000/somedata/{id}");
            var json = JsonConvert.SerializeObject(data);

            request.Content = new StringContent(json, Encoding.UTF8, "application/json");
            var response = await m_Client.SendAsync(request);

            string result = response.Content.ReadAsStringAsync().Result;

            Console.WriteLine($"{(int)response.StatusCode} {response.StatusCode} {result}");
        }
 public void sendUserData(SomeData infoe)
 {
     try
     {
         byte[] b = Helper.Serilize(infoe);
         writer.Write(1);
         writer.Write(b.Length);
         writer.Write(b);
         writer.Flush();
     }
     catch
     {
     }
 }
        public async Task <BaseResponse> Create(SomeData someData)
        {
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "");

            request.Content = new StringContent(JsonConvert.SerializeObject(someData), System.Text.Encoding.UTF8, "application/json");
            HttpResponseMessage httpResponse = await m_Client.SendAsync(request);

            BaseResponse response = new BaseResponse();

            response.ResponseStatus = (int)httpResponse.StatusCode;
            if (!httpResponse.IsSuccessStatusCode)
            {
                response.ResponseText = httpResponse.ReasonPhrase;
            }
            return(response);
        }
        static async Task Main(string[] args)
        {
            Crud   crud   = new Crud();
            string method = "";

            if (args.Length >= 1)
            {
                method = args[0];
                method = method.Trim().ToLower();
            }
            switch (method)
            {
            case "get":
            {
                bool   isSorted = false;
                string id       = "";
                if (args.Length >= 3 && args[1].Contains("sorted"))
                {
                    isSorted = args[2].Contains("True");
                }
                else if (args.Length >= 3 && args[1].Contains("id"))
                {
                    id = args[2];
                }
                if (id.Length == 0)
                {
                    Console.WriteLine(await crud.GetList(isSorted));
                }
                else
                {
                    Console.WriteLine(await crud.GetSingle(id));
                }
                break;
            }

            case "post":
            {
                SomeData some = new SomeData();
                if (args.Length >= 2)
                {
                    some = JsonConvert.DeserializeObject <SomeData>(args[1]);
                }
                Console.WriteLine(await crud.Create(some));
                break;
            }

            case "put":
            {
                string id = "";
                if (args.Length >= 3 && args[1].Contains("id"))
                {
                    id = args[2];
                }
                SomeData some = new SomeData();
                if (args.Length >= 4)
                {
                    some = JsonConvert.DeserializeObject <SomeData>(args[3]);
                }

                Console.WriteLine(await crud.Update(id, some));
                break;
            }

            case "delete":
            {
                string   id   = "";
                SomeData some = new SomeData();
                if (args.Length >= 3 && args[1].Contains("id"))
                {
                    id = args[2];
                }
                Console.WriteLine(await crud.Delete(id));
                break;
            }

            default:
            {
                Console.WriteLine("Invalid http method");
                break;
            }
            }
        }
Exemple #6
0
        void clientReceiver()
        {
            TcpClient socket = this.client.socket;

            int reqLen = Helper.getBytes("msg").Length;

            byte[] req = new byte[reqLen];

            NetworkStream netStream = socket.GetStream();

            while (socket.Connected)
            {
                socket.Client.Receive(req);

                BinaryReader streamR = new BinaryReader(netStream);

                if (Helper.getString(req).Equals("msg"))
                {
                    string       sender = streamR.ReadString();
                    DialogResult result =
                        MessageBox.Show(
                            "Accept the Incoming File from " + sender,
                            "Incoming Connection",
                            MessageBoxButtons.YesNo
                            );


                    if (result == DialogResult.Yes)
                    {
                        int dataLen = streamR.ReadInt32();

                        byte[] RecievedData = streamR.ReadBytes(dataLen);

                        //SaveFileName = streamR.ReadString();
                        Message m = (Message)Helper.deSerilize(RecievedData);

                        string sender_public_key = null;
                        foreach (SomeData item in clientList)
                        {
                            if (item.username.Equals(sender))
                            {
                                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                                rsa.FromXmlString(CA);
                                bool autinticated = rsa.VerifyHash(
                                    new SHA1Managed().ComputeHash(Helper.Serilize(item.info)),
                                    CryptoConfig.MapNameToOID("SHA1"),
                                    item.certificate
                                    );
                                if (autinticated)
                                {
                                    sender_public_key = item.info.publicKey;
                                }
                                else
                                {
                                    string msg = "not secure connection with " + sender + "\n do you want to complete process?";
                                    if (MessageBox.Show(msg, "warning", MessageBoxButtons.YesNo) == DialogResult.Yes)
                                    {
                                        sender_public_key = item.info.publicKey;
                                    }
                                    else
                                    {
                                        return;
                                    }
                                }
                                break;
                            }
                        }



                        if (AsymmetricEncryption.verifyMsg(sender_public_key, m))
                        {
                            String res = AsymmetricEncryption.PGPDecrypt(m.msg, rsaProvider.ToXmlString(true));

                            client.saveFile(res, "D://" + info.username + "/" + m.file_name);

                            FileStream stream = new FileStream("D://" + info.username + "/" +
                                                               Path.GetFileNameWithoutExtension(m.file_name) + ".sign", FileMode.OpenOrCreate);

                            stream.WriteByte(0);
                            stream.Write(Helper.getBytes(sender_public_key), 0, sender_public_key.Length);
                            stream.WriteByte(1);
                            stream.Write(m.signature, 0, m.signature.Length);
                            stream.Close();
                        }
                        else
                        {
                            MessageBox.Show("not reliable message");
                        }
                    }
                }
                else if (Helper.getString(req).Equals("lst"))
                {
                    clientList.Clear();
                    int len = streamR.ReadInt32();
                    for (int i = 0; i < len; i++)
                    {
                        SomeData itm = new SomeData();

                        int l = streamR.ReadInt32();
                        itm = (SomeData)Helper.deSerilize(streamR.ReadBytes(l));

                        clientList.Add(itm);
                    }

                    this.Invoke((MethodInvoker)(() => ShowData()));
                }
            }
        }
Exemple #7
0
        static async Task Main(string[] args)
        {
            Console.WriteLine("JSON data example {\"dataId\":5,\"weight\":5}");
            string request;
            Client myClient = new Client();

            while (true)
            {
                request = Console.ReadLine();
                string[] argz = request.ToLower().Split(' ');

                string requestType = argz[0];
                switch (requestType)
                {
                case "get":
                {
                    bool   isSorted = false;
                    string id       = "";
                    if (argz.Length >= 3 && argz[1].Contains("id"))
                    {
                        id = argz[2];
                        await myClient.GetById(id);
                    }
                    else if (argz.Length >= 3 && argz[1].Contains("sorted"))
                    {
                        isSorted = argz[2].Contains("true");
                        await myClient.GetAll(isSorted);
                    }
                    else
                    {
                        try
                        {
                            await myClient.GetAll(isSorted);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }
                    }

                    break;
                }

                case "post":
                {
                    SomeData newData = new SomeData();
                    if (argz.Length >= 2)
                    {
                        newData = JsonConvert.DeserializeObject <SomeData>(argz[1]);
                    }
                    try
                    {
                        await myClient.PostData(newData);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    break;
                }

                case "put":
                {
                    string id = "";
                    if (argz.Length >= 2)
                    {
                        id = argz[1];

                        SomeData updatedData = new SomeData();
                        if (argz.Length >= 3)
                        {
                            updatedData = JsonConvert.DeserializeObject <SomeData>(argz[2]);
                        }

                        try
                        {
                            await myClient.PutById(id, updatedData);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }
                    }

                    break;
                }

                case "delete":
                {
                    string id = "";
                    if (argz.Length >= 2)
                    {
                        id = argz[1];

                        try
                        {
                            await myClient.DeleteById(id);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }
                    }

                    break;
                }

                default:
                    Console.WriteLine("Bad request");
                    break;
                }
            }
        }
 public SingleSomeData() : base()
 {
     m_Data = new SomeData();
 }