How To Pass Data Using WebAPI In C# Windows Forms

How To Pass Data Using WebAPI In C# Windows Forms

In this article, you’ll understand how to create a Web API POST method in a Windows Forms application using C#.

Let’s go and create a Windows Forms project. then, open Visual Studio and click on Windows > New Project.

First, we are going to create a Web API POST method where we pass the data as a string.

In the following method, we will use the HttpWebRequest object to get or post the data from the URL.

Use the below namespaces.

using System;  
using System.IO;  
using System.Net;  
using System.Text; 

Now, let’s create the POST method.

public string WebApiPostMethod(string postData, string URL)  
{  
	string responseFromServer = "";  
	HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);  
	request.Method = "POST";  
	request.Credentials = CredentialCache.DefaultCredentials;  
	((HttpWebRequest)request).UserAgent =  
					  "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0)";  
	request.Accept = "/";  
	request.UseDefaultCredentials = true;  
	request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;  
	byte[] byteArray = Encoding.UTF8.GetBytes(postData);  
	request.ContentType = "application/x-www-form-urlencoded";  
	request.ContentLength = byteArray.Length;  
	Stream dataStream = request.GetRequestStream();  
	dataStream.Write(byteArray, 0, byteArray.Length);  
	dataStream.Close();  

	WebResponse response = request.GetResponse();  
	dataStream = response.GetResponseStream();  
	StreamReader reader = new StreamReader(dataStream);  
	responseFromServer = reader.ReadToEnd();  
	reader.Close();  
	dataStream.Close();  
	response.Close();  
	return responseFromServer;  
}

Below code, we will create a Web API GET method in Windows Forms application using C#.

public string webGetMethod(string URL)  
{  
	string jsonString = "";  
	HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);  
	request.Method = "GET";  
	request.Credentials = CredentialCache.DefaultCredentials;  
	((HttpWebRequest)request).UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0)";  
	request.Accept = "/";  
	request.UseDefaultCredentials = true;  
	request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;  
	request.ContentType = "application/x-www-form-urlencoded";  

	WebResponse response = request.GetResponse();  
	StreamReader sr = new StreamReader(response.GetResponseStream());  
	jsonString = sr.ReadToEnd();  
	sr.Close();  
	return jsonString;  
}  

After that, we’ll create the method to invoke the WebApiPostMethod and WebApiGetMethod.

private void callWebServices()  
{  
	string postData = "user_id=1234&passwd=stalingrad";  
	string URL = "http://xyz.xyz.amazonaws.com/index.php/samplefunctionname";  
	var data = WebApiPostMethod(postData, URL);  
	Console.WriteLine(data);  

	var getData = webGetMethod(URL);  
	Console.WriteLine(getData);  
} 

Using this method, we’ll get the result as a JSON string.

Thanks for the Reading article! I hope this get’s you started the app using .NET technologies. Take care.

Leave a Reply

Your email address will not be published. Required fields are marked *