Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net.Http;
  6. using System.Net.Http.Headers;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace TestApiClient
  11. {
  12. class Helper
  13. {
  14. static readonly HttpClient client = new HttpClient();
  15.  
  16. static string host = "http://chatapp.rickstoit.nl/api/";
  17. public static string api_token;
  18.  
  19. public static string responseout;
  20.  
  21.  
  22. public static async Task postDataAsync(string url, FormUrlEncodedContent content, bool bearer = true)
  23. {
  24. try
  25. {
  26. HttpClient client = new HttpClient();
  27. client.BaseAddress = new Uri(host);
  28. if (bearer)
  29. {
  30. client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", api_token);
  31. }
  32.  
  33. var result = await client.PostAsync(url, content);
  34. var contents = await result.Content.ReadAsStringAsync();
  35. dynamic user = JsonConvert.DeserializeObject(contents);
  36. Console.WriteLine(contents);
  37.  
  38. api_token = (string)user.api_token;
  39.  
  40. }
  41. catch(Exception e)
  42. {
  43. Console.WriteLine(e);
  44. }
  45. }
  46.  
  47. public static async Task getDataAsync(string url)
  48. {
  49. HttpClient client = new HttpClient();
  50. client.BaseAddress = new Uri(host);
  51. client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", api_token);
  52. var response = await client.GetStringAsync(url);
  53. Console.WriteLine(response);
  54. }
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement