Advertisement
IvanResh

Untitled

Mar 22nd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.00 KB | None | 0 0
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Net.Http;
  7. using System.Net.Http.Headers;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using Windows.UI.Popups;
  11.  
  12. namespace API
  13. {
  14.     public class APIRequestService
  15.     {
  16.         private const string baseUrl = "http://mysite.com/api/";
  17.  
  18.         static HttpClient client;
  19.         static string token;
  20.  
  21.         static APIRequestService()
  22.         {
  23.             client = new HttpClient();
  24.             client.MaxResponseContentBufferSize = 25600000;
  25.  
  26.         //Тут нужно достать токен (*тут достаётся не по-замариновски)
  27.             Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
  28.             if (localSettings.Values.ContainsKey("access_token"))
  29.             {
  30.                 token = localSettings.Values["access_token"].ToString();
  31.             }            
  32.            
  33.             client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
  34.         }
  35.  
  36.         public static async Task<T> Request<T>(string urlRequest, object parameters = null)
  37.         {
  38.             var url = new Uri(baseUrl + urlRequest);
  39.             HttpResponseMessage response = null;
  40.            
  41.             if (parameters != null)
  42.             {
  43.                 var json = JsonConvert.SerializeObject(parameters);
  44.                 var content = new StringContent(json, Encoding.UTF8, "application/json");
  45.                 if (!string.IsNullOrEmpty(token))
  46.                 {
  47.                     content.Headers.Add("token", token);
  48.                 }
  49.  
  50.                 try
  51.                 {
  52.                     response = await client.PostAsync(url, content);
  53.                 }
  54.                 catch (Exception ex)
  55.                 {
  56.             //Ошыбка
  57.                 }
  58.             }
  59.             else
  60.             {
  61.                 try
  62.                 {
  63.                     response = await client.GetAsync(url);
  64.                 }
  65.                 catch (Exception ex)
  66.                 {
  67.                 //Ошыбка
  68.                 }
  69.             }
  70.             if (response?.IsSuccessStatusCode)
  71.             {
  72.                     if (response.Content != null)
  73.                     {
  74.                         var stringResponse = await response.Content.ReadAsStringAsync();
  75.                         var jsonResponse = JToken.Parse(jS);
  76.                         try
  77.                         {
  78.                            T objResponse = jsonResponse.ToObject<T>();
  79.                             return objResponse;
  80.                         }
  81.                         catch(Exception ex)
  82.                         {
  83.                             //Ошыбка
  84.                         }
  85.                     }
  86.             }
  87.             else
  88.             {
  89.                 //Ошыбка
  90.             }
  91.  
  92.             return default(T);
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement