Wednesday, April 10, 2019

Call WEB API service using .Net code

This is a simple example calling Web API service from your .net application. Here i am following C# code.

We can achieve API service calling using WebClient.

Step 1
Refer System.Net.WebClient.dll in your project

Step2 - Source code

using System.Net;

public Class WebAPIHelper{

       public string callService() {

             WebClient client = new WebClient();
             client.Headers.Add("Content-type", "application/JSON");
              client.Encoding = Encoding.UTF8;

              //If your API and application is running in same IIS server use below credential otherwise
               //you have to set proper credential
              client.Credentials = CredentialCache.DefaultCredentials;

              string url = "https://url API url";
              string data = client.DownloadString(url);
              return data;
      }
      

}

1 comment:

Unknown said...

Very helpful information of web client feature.