Advertisement
Cieslin

ClientApiJSON_PUM

May 27th, 2018
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Http;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Net.Http.Headers;
  8.  
  9. namespace ClientApiJSON
  10. {
  11.     public class Account
  12.     {
  13.         public string Login { get; set; }
  14.         public string Password { get; set; }
  15.     }
  16.  
  17.  
  18.  
  19.     class Program
  20.     {
  21.  
  22.         static HttpClient client = new HttpClient();
  23.  
  24.         static async Task RunAsync()
  25.         {
  26.             client.BaseAddress = new Uri("http://localhost:63984//");
  27.             client.DefaultRequestHeaders.Accept.Clear();
  28.             client.DefaultRequestHeaders.Accept.Add(
  29.                 new MediaTypeWithQualityHeaderValue("application/json"));
  30.             try
  31.             {
  32.                 Account account = new Account
  33.                 {
  34.                     Login = "Jakis",
  35.                     Password = "InneFajneZajebisteDziala"
  36.                 };
  37.  
  38.                 var url = await CreateAccountAsync(account);
  39.             }
  40.             catch(Exception e)
  41.             {
  42.                 Console.WriteLine(e.Message);
  43.             }
  44.         }
  45.  
  46.         static async Task<Uri> CreateAccountAsync(Account account)
  47.         {
  48.             HttpResponseMessage response = await client.PostAsJsonAsync(
  49.                 "addaccount", account);
  50.             response.EnsureSuccessStatusCode();
  51.  
  52.             // return URI of the created resource.
  53.             return response.Headers.Location;
  54.         }
  55.  
  56.  
  57.         static void Main(string[] args)
  58.         {
  59.             RunAsync().GetAwaiter().GetResult();
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement