Esempio n. 1
0
 public static Task <T> Get <T>(ApiHost host) where T : class
 {
     using (var client = new HttpClient())
     {
         client.BaseAddress = new Uri(host.Domain);
         client.DefaultRequestHeaders.Accept.Clear();
         client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(host.DataMimeType));
         var response = client.GetAsync(host.ApiName + (string.IsNullOrEmpty(host.InputQueryString) ? "" : "?" + host.InputQueryString)).Result;
         if (response.IsSuccessStatusCode)
         {
             Task <string> json    = response.Content.ReadAsStringAsync();
             var           strJson = json.Result;
             if (!string.IsNullOrWhiteSpace(strJson))
             {
                 if (strJson.IndexOf("k__BackingField") > 0)
                 {
                     return(response.Content.ReadAsAsync <T>());
                 }
                 else
                 {
                     return(Task.Factory.StartNew(() => JsonConvert.DeserializeObject <T>(strJson)));
                 }
             }
             //return response.Content.ReadAsAsync<T>();
         }
         return(null);
     }
 }
Esempio n. 2
0
 public static Task <string> Get(ApiHost host)
 {
     using (var client = new HttpClient())
     {
         client.BaseAddress = new Uri(host.Domain);
         client.DefaultRequestHeaders.Accept.Clear();
         client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(host.DataMimeType));
         var response = client.GetAsync(host.ApiName + (string.IsNullOrEmpty(host.InputQueryString) ? "" : "?" + host.InputQueryString)).Result;
         if (response.IsSuccessStatusCode)
         {
             return(response.Content.ReadAsStringAsync());
         }
         return(null);
     }
 }
Esempio n. 3
0
 public static Task <TOut> Post <TIn, TOut>(ApiHost host, TIn postData)
     where TIn : class
     where TOut : class
 {
     using (var client = new HttpClient())
     {
         client.BaseAddress = new Uri(host.Domain);
         client.DefaultRequestHeaders.Accept.Clear();
         client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(host.DataMimeType));
         var response = client.PostAsJsonAsync <TIn>(host.ApiName, postData).Result;
         if (response.IsSuccessStatusCode)
         {
             Task <string> json    = response.Content.ReadAsStringAsync();
             var           strJson = json.Result;
             if (!string.IsNullOrWhiteSpace(strJson))
             {
                 if (strJson.IndexOf("k__BackingField") > 0)
                 {
                     return(response.Content.ReadAsAsync <TOut>());
                 }
                 else
                 {
                     return(Task.Factory.StartNew(() => JsonConvert.DeserializeObject <TOut>(strJson)));
                 }
                 //var task = Task.Factory.StartNew(() => JsonConvert.DeserializeObject<TOut>(strJson));
                 //if (task.Result == null)
                 //{
                 //    return response.Content.ReadAsAsync<TOut>();
                 //}
                 //else
                 //{
                 //    return task;
                 //}
             }
             //return response.Content.ReadAsAsync<TOut>();
         }
         return(null);
     }
 }