PP Smartwork

PP Smartwork

  • NA
  • 8
  • 17.8k

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.

Answers (3)