Introduction
The API Web service call function in windows application is highly secured. This is because of no data available on the local computer, so all are services oriented. The records insert and update, delete, select has been [HttpGet], [HttpPut], [HttpPost] and [HttpDelete] oriented functions.
Step 1
Create a new project.
Step 2
Choose Windows Desktop, and Select the Windows Forms App (.NET Framework)
Step 3
Design the Windows application.
Step 4 - [HttpGet], [HttpPut], [HttpPost] and [HttpDelete]
Declare the Headers and add on dll Reference (System.Net.Http.Formatting, Newtonsoft.json)
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Windows.Forms;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Net;
- using System.IO;
- using Newtonsoft.Json;
Get all the data in Gridview List: [HttpGet]
- private void GetData()
- {
-
- HttpClient client = new HttpClient();
- client.BaseAddress = new Uri("http://localhost:2514/");
- client.DefaultRequestHeaders.Accept.Add(
- new MediaTypeWithQualityHeaderValue("application/json"));
-
- HttpResponseMessage response = client.GetAsync("api/values").Result;
-
- if (response.IsSuccessStatusCode)
- {
- var users = response.Content.ReadAsAsync<IEnumerable<Users>>().Result;
- dataGridView1.DataSource = users;
- }
- else
- {
- MessageBox.Show("Error Code" + response.StatusCode + " : Message - " + response.ReasonPhrase);
- }
-
- }
Call the Get data Function in Form1()
- public Form1()
- {
- InitializeComponent();
- GetData();
- }
Refer the below screenshot for thedata grid view list:
Post the data in Gridview List: [HttpPost]
- private void button1_Click(object sender, EventArgs e)
- {
- Users objProduct = new Users();
- objProduct.Username = textBox1.Text;
- objProduct.Mobile = textBox2.Text;
- string json = JsonConvert.SerializeObject(objProduct);
-
- var baseAddress = "http://localhost:2514/api/values/GetMobileInsert?Username=" + textBox1.Text + "&Mobile=" + textBox2.Text + "";
-
- var http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));
- http.Accept = "application/json";
- http.ContentType = "application/json";
- http.Method = "POST";
-
- string parsedContent = json;
- ASCIIEncoding encoding = new ASCIIEncoding();
- Byte[] bytes = encoding.GetBytes(parsedContent);
-
- Stream newStream = http.GetRequestStream();
- newStream.Write(bytes, 0, bytes.Length);
- newStream.Close();
-
- var response = http.GetResponse();
-
- var stream = response.GetResponseStream();
-
- GetData();
- }
Refer to the below screenshot for the data grid view Insert:
Put the data in Gridview List: [HttpPut]
- private void button2_Click(object sender, EventArgs e)
- {
- Users objProduct = new Users();
- objProduct.Username = textBox1.Text;
- objProduct.Mobile = textBox2.Text;
- string json = JsonConvert.SerializeObject(objProduct);
-
- var baseAddress = "http://localhost:2514/api/values/GetMobileUpdate?ID="+textBox3.Text+"&Username=" + textBox1.Text + "&Mobile=" + textBox2.Text + "";
-
- var http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));
- http.Accept = "application/json";
- http.ContentType = "application/json";
- http.Method = "PUT";
-
- string parsedContent = json;
- ASCIIEncoding encoding = new ASCIIEncoding();
- Byte[] bytes = encoding.GetBytes(parsedContent);
-
- Stream newStream = http.GetRequestStream();
- newStream.Write(bytes, 0, bytes.Length);
- newStream.Close();
-
- var response = http.GetResponse();
-
- var stream = response.GetResponseStream();
-
- GetData();
- }
Refer to the below screenshot for data grid view Update:
Get the Randomly selected value in data GridView:
- private void GetselectData()
- {
- HttpClient client = new HttpClient();
- client.BaseAddress = new Uri("http://localhost:2514/");
- client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
-
- var id = textBox3.Text;
-
- var url = "api/values/GetMobile?ID=" + id;
-
- HttpResponseMessage response = client.GetAsync(url).Result;
-
- if (response.IsSuccessStatusCode)
- {
- var result = response.Content.ReadAsStringAsync().Result;
- var s = Newtonsoft.Json.JsonConvert.DeserializeObject(result);
- MessageBox.Show(s.ToString());
- dataGridView1.DataSource = s;
- }
- else
- {
- MessageBox.Show("Fail");
- }
-
- }
Refer to the below screenshot for a randomly selected value in data GridView:
Delete the data in Gridview List: [HttpDelete]
- private void button4_Click(object sender, EventArgs e)
- {
- Users objProduct = new Users();
- objProduct.Username = textBox1.Text;
- objProduct.Mobile = textBox2.Text;
- string json = JsonConvert.SerializeObject(objProduct);
-
- var baseAddress = "http://localhost:2514/api/values?ID=" + textBox3.Text + "";
-
- var http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));
- http.Accept = "application/json";
- http.ContentType = "application/json";
- http.Method = "DELETE";
-
- string parsedContent = json;
- ASCIIEncoding encoding = new ASCIIEncoding();
- Byte[] bytes = encoding.GetBytes(parsedContent);
-
- Stream newStream = http.GetRequestStream();
- newStream.Write(bytes, 0, bytes.Length);
- newStream.Close();
-
- var response = http.GetResponse();
-
- var stream = response.GetResponseStream();
-
- GetData();
- }
Refer to the below screenshot for data grid view Delete:
Summary
The web API call function is used in windows applications and is one of the best options for high-level data security. I hope this method helps you to API Web service call in a Windows application.