Ritu

Ritu

  • 1.3k
  • 344
  • 37.2k

how to get tokens

May 18 2021 12:20 PM
I have to create web api in asp.net core console application and want to check api is created?
 
and want to create token for a web api but unable to create it.
 
please help me to create token and consume api in console app
  1. // code for token  
  2. string result = string.Empty;  
  3. HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(baseAddress);  
  4. httpWebRequest.ContentType = "application/json; charset=utf-8";  
  5. httpWebRequest.Method = "Get";  
  6. httpWebRequest.Accept = "application/json";  
  7. httpWebRequest.Headers.Add("Authorization""Bearer " + accessToken);  
  8. // method for consume api  
  9. public string GET(string url)  
  10. {  
  11. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);  
  12. StreamReader reader;  
  13. try  
  14. {  
  15. WebResponse response = request.GetResponse();  
  16. using (Stream responseStream = response.GetResponseStream())  
  17. {  
  18. reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);  
  19. return reader.ReadToEnd();  
  20. }  
  21. }  
  22. catch (WebException ex)  
  23. {  
  24. WebResponse errorResponse = ex.Response;  
  25. using (Stream responseStream = errorResponse.GetResponseStream())  
  26. {  
  27. reader = new StreamReader(responseStream, System.Text.Encoding.GetEncoding("utf-8"));  
  28. String errorText = reader.ReadToEnd();  
  29. }  
  30. throw;  
  31. }  
  32. }

Answers (1)