コード例 #1
0
 public virtual IEnumerable <T> Get(IFilter filter)
 {
     using (var client = new HttpClient())
     {
         client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
             "Basic", Convert.ToBase64String(
                 System.Text.ASCIIEncoding.ASCII.GetBytes(
                     $"{login}:{password}")));
         var response = client.GetAsync(this.url + (filter == null ? "" : filter.BuildQueryString())).Result;
         if (response.IsSuccessStatusCode)
         {
             return(JsonConvert.DeserializeObject <IEnumerable <T> >(response.Content.ReadAsStringAsync().Result));
         }
         else
         {
             if (response.Content.ReadAsStringAsync().Result != String.Empty && response.StatusCode == HttpStatusCode.BadRequest)
             {
                 var error = JsonConvert.DeserializeObject <Error>(response.Content.ReadAsStringAsync().Result);
                 throw new RestClientException(error.error);
             }
             else if (response.Content.ReadAsStringAsync().Result != String.Empty && response.StatusCode == HttpStatusCode.Unauthorized)
             {
                 throw new RestClientException("Ошибка авторизации веб-сервиса");
             }
             else
             {
                 throw new RestClientException("При выполнении запроса произошла ошибка");
             }
         }
     }
 }