private async void SendDataToApi(studentdata per) { using (HttpClient client = new HttpClient()) { client.BaseAddress = new Uri("http://localhost:13713/"); string endPoint = "api/person"; string json = JsonConvert.SerializeObject(per); var data = new StringContent(content: json, encoding: Encoding.UTF8, mediaType: "application/json"); var res = await client.PostAsync(endPoint, data); MessageBox.Show("Data Inserted"); } }
/* private void putdata() * { * using (var client = new HttpClient()) * { * // Person p = new Person { person_name = "Sourav", = "Kayal" }; * client.BaseAddress = new Uri("http://localhost:1565/"); * var response = client.PutAsJsonAsync("api/person", p).Result; * if (response.IsSuccessStatusCode) * { * Console.Write("Success"); * } * } * */ private void getdata(int id) { HttpClient client = new HttpClient(); client.BaseAddress = new Uri("http://localhost:13713/"); // Add an Accept header for JSON format. client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // List all Names. HttpResponseMessage response = client.GetAsync("api/person/4").Result; // Blocking call! if (response.IsSuccessStatusCode) { var products = response.Content.ReadAsStringAsync().Result; studentdata crs = JsonConvert.DeserializeObject <studentdata>(products); MessageBox.Show(crs.Id.ToString()); } }