Consume Web API Using WebClient In C#

We can Get and Post data from a Web API using Web client. Web client provides common methods for sending and receiving data from Server. Here, I have not used any authentication and authorization mechanism. This is simple - it's just for sending and receiving data from API.
 
First, you have to assign the API Endpoint on a variable. Here, I have created one static class to assign the API Endpoint. 
  1. public static class StaticItems  
  2. {  
  3.    public static string EndPoint = "http://localhost:8088/api/";  
  4. }   
Here, I have also created a class of name City.
  1. public class CityInfo  
  2. {  
  3.    public int CityID { getset; }  
  4.    public int CountryID { getset; }  
  5.    public int StateID { getset; }  
  6.    public string Name { getset; }  
  7.    public decimal Latitude { getset; }  
  8.    public decimal Longitude { getset; }  
  9. }  

Receiving Data from API

 
This function retrieves data from API. Here, this function returns the list of cities. The data received from the API is in JSON format so I have deserialized that JSON data to an Object List. The Keyword DownloadString is used for retrieving data from Server.
  1. public List < CityInfo > CityGet() {  
  2.     try {  
  3.         using(WebClient webClient = new WebClient()) {  
  4.             webClient.BaseAddress = StaticItems.EndPoint;  
  5.             var json = webClient.DownloadString("City/CityGetForDDL");  
  6.             var list = JsonConvert.DeserializeObject < List < CityInfo >> (json);  
  7.             return list.ToList();  
  8.         }  
  9.     } catch (WebException ex) {  
  10.         throw ex;  
  11.     }  
  12. }  

Sending Data to Api

 
This function sends data to the API. Here, I have converted the object of the city into JSON.
 
The keyword UploadString is used for sending data to the Server. 
  1. public ReturnMessageInfo CitySave(CityInfo city) {  
  2.     ReturnMessageInfo result = new ReturnMessageInfo();  
  3.     try {  
  4.         using(WebClient webClient = new WebClient()) {  
  5.             webClient.BaseAddress = StaticItems.EndPoint;  
  6.             var url = "City/CityAddUpdate";  
  7.             webClient.Headers.Add("user-agent""Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");  
  8.             webClient.Headers[HttpRequestHeader.ContentType] = "application/json";  
  9.             string data = JsonConvert.SerializeObject(city);  
  10.             var response = webClient.UploadString(url, data);  
  11.             result = JsonConvert.DeserializeObject < ReturnMessageInfo > (response);  
  12.             return result;  
  13.         }  
  14.     } catch (Exception ex) {  
  15.         throw ex;  
  16.     }  
  17. }  

Conclusion

 
Web client is easy to use for consuming the Web API. You can also use httpClient instead of WebClient. I hope you learned a little more about Web client and how a Web Client is used for sending and receiving data from Web API in C#. Let me know your queries in the comments section.