Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class HttpRequest
- {
- public void MakeReqeust()
- {
- try
- {
- using (var client = new HttpClient())
- {
- string baseUrl = ConfigurationManager.AppSettings[CommonConstants.C4ProductSubstitution.BaseUrl];
- string getTokenEndpointUrl = string.Format("{0}/{1}", baseUrl, CommonConstants.C4ProductSubstitution.TokenUrl);
- var formData = new Dictionary<string, string>
- {
- {
- CommonConstants.C4ProductSubstitution.UsernameKey,
- ConfigurationManager.AppSettings[CommonConstants.C4ProductSubstitution.Username].ToString()
- },
- {
- CommonConstants.C4ProductSubstitution.PasswordKey,
- ConfigurationManager.AppSettings[CommonConstants.C4ProductSubstitution.Password].ToString()
- },
- {
- CommonConstants.C4ProductSubstitution.GrantTypeKey,
- ConfigurationManager.AppSettings[CommonConstants.C4ProductSubstitution.GrantType].ToString()
- }
- };
- var content = new FormUrlEncodedContent(formData);
- ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
- var result = client.PostAsync(getTokenEndpointUrl, content).GetAwaiter().GetResult();
- if (result.StatusCode == System.Net.HttpStatusCode.OK)
- {
- var tokenData = result.Content.ReadAsStringAsync().Result;
- var json = JObject.Parse(tokenData);
- var token = json[CommonConstants.C4ProductSubstitution.TokenKey].ToString();
- Console.WriteLine(token);
- }
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- }
- }
- }
- public class CommonConstants
- {
- public static class C4ProductSubstitution
- {
- public static readonly string BaseUrl = "C4ProductsSubstituteUrl";
- public static readonly string TokenUrl = "token";
- public static readonly string Username = "C4ProductsSubstituteUsername";
- public static readonly string UsernameKey = "username";
- public static readonly string Password = "C4ProductsSubstitutePassword";
- public static readonly string PasswordKey = "password";
- public static readonly string GrantType = "C4ProductsSubstituteGrantType";
- public static readonly string GrantTypeKey = "grant_type";
- public static readonly string AuthorizationKey = "Authorization";
- public static readonly string ProductIdKey = "Code";
- public static readonly string TokenKey = "access_token";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement