Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Text;
- using System.Threading.Tasks;
- namespace TestApiClient
- {
- class Helper
- {
- static readonly HttpClient client = new HttpClient();
- static string host = "http://chatapp.rickstoit.nl/api/";
- public static string api_token;
- public static string responseout;
- public static async Task postDataAsync(string url, FormUrlEncodedContent content, bool bearer = true)
- {
- try
- {
- HttpClient client = new HttpClient();
- client.BaseAddress = new Uri(host);
- if (bearer)
- {
- client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", api_token);
- }
- var result = await client.PostAsync(url, content);
- var contents = await result.Content.ReadAsStringAsync();
- dynamic user = JsonConvert.DeserializeObject(contents);
- Console.WriteLine(contents);
- api_token = (string)user.api_token;
- }
- catch(Exception e)
- {
- Console.WriteLine(e);
- }
- }
- public static async Task getDataAsync(string url)
- {
- HttpClient client = new HttpClient();
- client.BaseAddress = new Uri(host);
- client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", api_token);
- var response = await client.GetStringAsync(url);
- Console.WriteLine(response);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement