Advertisement
Guest User

Untitled

a guest
Feb 5th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. public class HttpRequest
  2. {
  3. public void MakeReqeust()
  4. {
  5. try
  6. {
  7. using (var client = new HttpClient())
  8. {
  9. string baseUrl = ConfigurationManager.AppSettings[CommonConstants.C4ProductSubstitution.BaseUrl];
  10. string getTokenEndpointUrl = string.Format("{0}/{1}", baseUrl, CommonConstants.C4ProductSubstitution.TokenUrl);
  11. var formData = new Dictionary<string, string>
  12. {
  13. {
  14. CommonConstants.C4ProductSubstitution.UsernameKey,
  15. ConfigurationManager.AppSettings[CommonConstants.C4ProductSubstitution.Username].ToString()
  16. },
  17. {
  18. CommonConstants.C4ProductSubstitution.PasswordKey,
  19. ConfigurationManager.AppSettings[CommonConstants.C4ProductSubstitution.Password].ToString()
  20. },
  21. {
  22. CommonConstants.C4ProductSubstitution.GrantTypeKey,
  23. ConfigurationManager.AppSettings[CommonConstants.C4ProductSubstitution.GrantType].ToString()
  24. }
  25. };
  26. var content = new FormUrlEncodedContent(formData);
  27. ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
  28. var result = client.PostAsync(getTokenEndpointUrl, content).GetAwaiter().GetResult();
  29.  
  30. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  31. {
  32. var tokenData = result.Content.ReadAsStringAsync().Result;
  33. var json = JObject.Parse(tokenData);
  34. var token = json[CommonConstants.C4ProductSubstitution.TokenKey].ToString();
  35. Console.WriteLine(token);
  36. }
  37. }
  38. }
  39. catch (Exception e)
  40. {
  41. Console.WriteLine(e.ToString());
  42. }
  43. }
  44. }
  45.  
  46. public class CommonConstants
  47. {
  48. public static class C4ProductSubstitution
  49. {
  50. public static readonly string BaseUrl = "C4ProductsSubstituteUrl";
  51. public static readonly string TokenUrl = "token";
  52. public static readonly string Username = "C4ProductsSubstituteUsername";
  53. public static readonly string UsernameKey = "username";
  54. public static readonly string Password = "C4ProductsSubstitutePassword";
  55. public static readonly string PasswordKey = "password";
  56. public static readonly string GrantType = "C4ProductsSubstituteGrantType";
  57. public static readonly string GrantTypeKey = "grant_type";
  58. public static readonly string AuthorizationKey = "Authorization";
  59. public static readonly string ProductIdKey = "Code";
  60. public static readonly string TokenKey = "access_token";
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement