Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.82 KB | None | 0 0
  1. using GAMongo.Service.DynamicsCRM.Contract;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Net.Http;
  5. using System.Net.Http.Headers;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace GAMongo.Service.DynamicsCRM
  10. {
  11.     public static class DynamicsCrmConnector
  12.     {
  13.         public static HttpClient GetHttpClient(string url, string username, string password)
  14.         {
  15.             try
  16.             {
  17.                 HttpMessageHandler messageHandler;
  18.  
  19.                 messageHandler = new OAuthMessageHandler(url, new TokenRequest() { Username = username, Password = password }, new HttpClientHandler());
  20.  
  21.                 HttpClient httpClient = new HttpClient(messageHandler)
  22.                 {
  23.                     BaseAddress = new Uri(url),
  24.                     Timeout = new TimeSpan(0, 0, 30)  //30 sec
  25.                 };
  26.                 return httpClient;
  27.             }
  28.             catch (Exception)
  29.             {
  30.                 throw;
  31.             }
  32.         }
  33.     }
  34.     class OAuthMessageHandler : DelegatingHandler
  35.     {
  36.         private AuthenticationHeaderValue authHeader;
  37.  
  38.         public OAuthMessageHandler(string serviceUrl, TokenRequest tokenRequest,
  39.                 HttpMessageHandler innerHandler)
  40.             : base(innerHandler)
  41.         {
  42.             var httpContent = new StringContent(JsonConvert.SerializeObject(tokenRequest), Encoding.UTF8, "application/json");
  43.  
  44.             using (HttpClient client = new HttpClient())
  45.             {
  46.                 var response = client.PostAsync(serviceUrl + "/api/token", httpContent).Result;
  47.  
  48.                 if (response.IsSuccessStatusCode)
  49.                 {
  50.                     var token = JsonConvert.DeserializeObject<string>(response.Content.ReadAsStringAsync().Result);
  51.                     authHeader = new AuthenticationHeaderValue("Bearer", token);
  52.                 }
  53.             }
  54.         }
  55.  
  56.         protected override Task<HttpResponseMessage> SendAsync(
  57.          HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
  58.         {
  59.             request.Headers.Authorization = authHeader;
  60.             return base.SendAsync(request, cancellationToken);
  61.         }
  62.     }
  63. }
  64.  
  65.  
  66. /Usage
  67.  
  68. public ContactByCardResponse GetContactByCard(ContactByCardRequest request)
  69.         {
  70.             using (HttpClient client = DynamicsCrmConnector.GetHttpClient(_setting.Url, _setting.Username, _setting.Password))
  71.             {
  72.                 var httpContent = new StringContent(JsonConvert.SerializeObject(request, Formatting.Indented), Encoding.UTF8, "application/json");
  73.                 var response = client.PostAsync(_setting.Url + "/api/Customer/ContactByCard", httpContent).Result;
  74.                 return JsonConvert.DeserializeObject<ContactByCardResponse>(response.Content.ReadAsStringAsync().Result);
  75.             }
  76.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement