Пример #1
0
        public async Task Add(TodoItem item)
        {
            var httpClient = new HttpClient();
            var uri = new Uri("http://localhost:5555/todo/api/v1.0/tasks");

            string jsonToPost = new JavaScriptSerializer().Serialize(new
            {
                title = item.Title,
                description = item.Description,
                imasge = item.Image
            });

            //httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            try
            {
                HttpResponseMessage response = await httpClient.PostAsync(uri,
                   new StringContent(jsonToPost, Encoding.UTF8, "application/json"));

                if (response.IsSuccessStatusCode)
                {
                    var result = await response.Content.ReadAsStringAsync();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                throw;
            }
        }
Пример #2
0
        public async Task Update(TodoItem item)
        {
            var httpClient = new HttpClient();
            var uri = new Uri("http://localhost:5555/todo/api/v1.0/tasks/" + item.Id);

            //Dictionary<string, string> jsonObj = new Dictionary<string, string>();
            //jsonObj.Add("title", item.Title);
            //jsonObj.Add("description", item.Description);
            //jsonObj.Add("image", item.Image);
            //jsonObj.Add("done", item.Done.ToString());

            //JavaScriptSerializer serializer = new JavaScriptSerializer();
            //var jsonContent = new StringContent(serializer.Serialize(jsonObj), Encoding.UTF8);
            //jsonContent.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");
            //httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            string jsonToPut = new JavaScriptSerializer().Serialize(new
            {
                title = item.Title,
                description = item.Description,
                image = item.Image,
                done = item.Done
            });

            try
            {
                HttpResponseMessage response = await httpClient.PutAsync(uri,
                        new StringContent(jsonToPut, Encoding.UTF8, "application/json"));

                if (response.IsSuccessStatusCode)
                {
                    var result = await response.Content.ReadAsStringAsync();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                throw;
            }
        }