PP Smartwork

PP Smartwork

  • 2k
  • 8
  • 7k

how to call web api in asp.net c# code behind

Mar 27 2018 10:24 AM
  1. string json = "{\"clientName\":\"test12\"}";  
  2. var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://test/api/ClientID.pdo");  
  3.  httpWebRequest.ContentType = "application/json";  
  4.  httpWebRequest.Accept = "application/json";   
  5. httpWebRequest.Method = "POST";  
  6. string credentials = "XYZ:XYZ";   
  7. httpWebRequest.Headers.Add("Authorization""basic" + credentials);  
  8. using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))  
  9.  {   
  10.       streamWriter.Write(json);   
  11.       streamWriter.Flush();   
  12.       streamWriter.Close();  
  13.  }  
  14. var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();  
  15.  using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))  
  16.  {   
  17.     var result = streamReader.ReadToEnd();  
  18.  var res = new JavaScriptSerializer().Deserialize<Response>(result);   
  19.     int clientID = res.clientID; //return res;    
  20. }  
I am getting clientID=0.
 
Plz help me.

Brought to you by:

Answers (3)