In Focus

Pass Data Using WebAPI In C# Windows Forms

In this blog, we will learn how to pass the data using Web API in a Windows application using C#.

Here, I have described the simplest way to create a Web API POST method in a Windows application using C#.
                                                                                                         
First, we have to create a Web API POST method where we pass the data as a string.
 
In this method, we will use the HttpWebRequest object to get or post the data from the URL. 
                                                                                         
Use the below namespaces.
  1. using System;  
  2. using System.IO;  
  3. using System.Net;  
  4. using System.Text;  
 Now, let us create the POST method.
  1. public string webPostMethod(string postData, string URL)  
  2.         {  
  3.             string responseFromServer = "";  
  4.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);  
  5.             request.Method = "POST";  
  6.             request.Credentials = CredentialCache.DefaultCredentials;  
  7.             ((HttpWebRequest)request).UserAgent =  
  8.                               "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0)";  
  9.             request.Accept = "/";  
  10.             request.UseDefaultCredentials = true;  
  11.             request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;  
  12.             byte[] byteArray = Encoding.UTF8.GetBytes(postData);  
  13.             request.ContentType = "application/x-www-form-urlencoded";  
  14.             request.ContentLength = byteArray.Length;  
  15.             Stream dataStream = request.GetRequestStream();  
  16.             dataStream.Write(byteArray, 0, byteArray.Length);  
  17.             dataStream.Close();  
  18.   
  19.             WebResponse response = request.GetResponse();  
  20.             dataStream = response.GetResponseStream();  
  21.             StreamReader reader = new StreamReader(dataStream);  
  22.             responseFromServer = reader.ReadToEnd();  
  23.             reader.Close();  
  24.             dataStream.Close();  
  25.             response.Close();  
  26.             return responseFromServer;  
  27.         }  
Here, we will create a Web API GET method in Windows application using C#.
  1. public string webGetMethod(string URL)  
  2.         {  
  3.             string jsonString = "";  
  4.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);  
  5.             request.Method = "GET";  
  6.             request.Credentials = CredentialCache.DefaultCredentials;  
  7.             ((HttpWebRequest)request).UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0)";  
  8.             request.Accept = "/";  
  9.             request.UseDefaultCredentials = true;  
  10.             request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;  
  11.             request.ContentType = "application/x-www-form-urlencoded";  
  12.   
  13.             WebResponse response = request.GetResponse();  
  14.             StreamReader sr = new StreamReader(response.GetResponseStream());  
  15.             jsonString = sr.ReadToEnd();  
  16.             sr.Close();  
  17.             return jsonString;  
  18.         }  
Now, let's create the method to invoke the webPostMethod and webGetMethod.
  1. private void callWebServices()  
  2.         {  
  3.             string postData = "user_id=25689&passwd=123456";  
  4.             string URL = "http://xxx-xx-xxx-xx-xxx.xxxxxxx-x.amazonaws.com/index.php/somefunctionname";  
  5.             var data = webPostMethod(postData, URL);  
  6.             Console.WriteLine(data);  
  7.   
  8.             var getData = webGetMethod(URL);  
  9.             Console.WriteLine(getData);  
  10.         }  
Here, we get the result as a JSON string.
 
I hope this is useful for all readers. Happy Coding!