public async System.Threading.Tasks.Task Auth() { Debug.WriteLine($"Соединение с CRM"); handler = new HttpClientHandler { CookieContainer = new CookieContainer(), UseCookies = true, UseDefaultCredentials = true }; client = new HttpClient(handler); var requestParams = new Dictionary <string, string> { { "USER_LOGIN", this.login }, { "USER_HASH", this.pass } }; var content = new FormUrlEncodedContent(requestParams); var request = await client.PostAsync(endPoint.Auth, content); var response = await request.Content.ReadAsStringAsync(); if (!request.IsSuccessStatusCode) { var error = JsonConvert.DeserializeObject <AuthResponse>(response); var exception = new AmoCrmHttpException(error); throw exception; } AuthInfo = JsonConvert.DeserializeObject <AuthResponse>(response).Content; ServerTimeDiff = (DateTime.Now - new DateTime().FromTimestamp(AuthInfo.ServerTime)) * -1; lock (locker) { cookiesLiveTime = DateTime.Now; } }
public async Task <AccountInfo> GetCrmInfo() { if (Account != null) { return(Account); } client = await GetClient(); var endPoint = GetEndPoint <AccountInfo>(); var request = await client.GetAsync(endPoint); var response = await request.Content.ReadAsStringAsync(); if (!request.IsSuccessStatusCode) { var error = JsonConvert.DeserializeObject <AuthResponse>(response); var exception = new AmoCrmHttpException(error); throw exception; } Account = JsonConvert.DeserializeObject <AccountInfo>(response); return(Account); }
public static async Task <string> GetResultAsync(this HttpClient client, string url) { var request = await client.GetAsync(url).ConfigureAwait(false); var response = await request.Content.ReadAsStringAsync().ConfigureAwait(false); if (!request.IsSuccessStatusCode) { var error = JsonConvert.DeserializeObject <AuthResponse>(response); var exception = new AmoCrmHttpException(error); throw exception; } return(response); }
public static async Task <string> PostResultAsync(this HttpClient client, Uri url, System.Object @object) { var objToJson = JsonConvert.SerializeObject(@object, new PostJsonSerializerSettings().GetSerializeSetting()); var stringContent = new StringContent(objToJson.ToString()); var request = await client.PostAsync(url, stringContent).ConfigureAwait(false); var response = await request.Content.ReadAsStringAsync().ConfigureAwait(false); if (!request.IsSuccessStatusCode) { var error = JsonConvert.DeserializeObject <AuthResponse>(response); var exception = new AmoCrmHttpException(error); throw exception; } return(response); }