How to Call Rest API in ASP.NET

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Net;  
  6. using System.IO;  
  7. using System.Data;  
  8. using System.Data.Common;  
  9. using Golive_Web.Controller;  
  10. namespace Golive.Controller  
  11. {  
  12.     public class APITEXT  
  13.     {  
  14.         Database database = new Database("GoliveAPI");  
  15.         public DataTable GetAPI()  
  16.         {  
  17.             DbCommand cmd = database.GetStoredPocCommand("SP_TExt");  
  18.             DataTable dtApi = database.ExecuteDataTable(cmd);  
  19.             return dtApi;  
  20.         }  
  21.         public string GetData(String mobileNo, String ID, String APILink)  
  22.         {  
  23.             try  
  24.             {  
  25.                 string XML = "<AccDtls>" + "<Request>" + "<RequestUUID>Req_" + ID + "</RequestUUID>" + "<ServiceRequestId>AcctInq</ServiceRequestId>" + "<ServiceRequestVersion>10.2</ServiceRequestVersion>" + "<ChannelId>CTS</ChannelId>" + "<AccID>" + mobileNo + "</AccID> " + "</Request>" + "</AccDtls>";  
  26.                 const string url = "http://100.98.2.208:6070/rbl/CTSFin";  
  27.                 HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);  
  28.                 request.ContentType = "application/xml"//set the content type to JSON  
  29.                 request.Method = "POST"//make an HTTP POST  
  30.                 #  
  31.                 region test# endregion  
  32.                 using(var streamWriter = new StreamWriter(request.GetRequestStream()))  
  33.                     {  
  34.                         var resToWrite = XML;  
  35.                         streamWriter.Write(resToWrite);  
  36.                         streamWriter.Flush();  
  37.                         streamWriter.Close();  
  38.                     }  
  39.                     // Get the response.  
  40.                 WebResponse response = request.GetResponse();  
  41.                 var streamReader = new StreamReader(response.GetResponseStream());  
  42.                 string result = streamReader.ReadToEnd();  
  43.                 return result;  
  44.             }  
  45.             catch (Exception)  
  46.             {  
  47.                 throw;  
  48.             }  
  49.         }  
  50.     }  
  51. }