Advertisement
Guest User

habababa

a guest
Mar 15th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.61 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Net.Http;
  4. using System.Net.Http.Headers;
  5. using System.Threading.Tasks;
  6. using TanksOnline.ProjektPZ.Server.Domain.Entities;
  7. using TanksOnline.ProjektPZ.Server.Domain.Enums;
  8.  
  9. namespace HttpClientSample
  10. {
  11.     class Program
  12.     {
  13.         static HttpClient client = new HttpClient();
  14.  
  15.         static void ShowUser(User user)
  16.         {
  17.             Console.WriteLine($"Name: {user.Name}\tEmail: {user.Email}\tBil mówi nie - passw :> {user.Password}");
  18.         }
  19.  
  20.         static async Task<Uri> CreateUserAsync(User user)
  21.         {
  22.             HttpResponseMessage response = await client.PostAsJsonAsync("api/users", user);
  23.             response.EnsureSuccessStatusCode();
  24.  
  25.             // return URI of the created resource.
  26.             return response.Headers.Location;
  27.         }
  28.  
  29.         static async Task<User> GetUserAsync(string path)
  30.         {
  31.             User product = null;
  32.             HttpResponseMessage response = await client.GetAsync(path);
  33.             if (response.IsSuccessStatusCode)
  34.             {
  35.                 product = await response.Content.ReadAsAsync<User>();
  36.             }
  37.             return product;
  38.         }
  39.  
  40.         static async Task<Product> UpdateProductAsync(Product product)
  41.         {
  42.             HttpResponseMessage response = await client.PutAsJsonAsync($"api/products/{product.Id}", product);
  43.             response.EnsureSuccessStatusCode();
  44.  
  45.             // Deserialize the updated product from the response body.
  46.             product = await response.Content.ReadAsAsync<Product>();
  47.             return product;
  48.         }
  49.  
  50.         static async Task<HttpStatusCode> DeleteProductAsync(string id)
  51.         {
  52.             HttpResponseMessage response = await client.DeleteAsync($"api/products/{id}");
  53.             return response.StatusCode;
  54.         }
  55.  
  56.         static void Main()
  57.         {
  58.             RunAsync().Wait();
  59.         }
  60.  
  61.         static async Task RunAsync()
  62.         {
  63.             client.BaseAddress = new Uri("http://localhost:21021/");
  64.             client.DefaultRequestHeaders.Accept.Clear();
  65.             client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  66.  
  67.             try
  68.             {
  69.                 // Create a new user
  70.                 User user = new User
  71.                 {
  72.                     Email = "test",
  73.                     Name = "test_name",
  74.                     Password = "awesomepassword",
  75.                     Status = UserStatus.Offline,
  76.                     TankInfo = new TankInfo() { Name = "RudyKurwaNa102" },
  77.                     UserScore = new UserScore()
  78.                 };
  79.  
  80.                 var url = await CreateUserAsync(user);
  81.                 Console.WriteLine($"Created at {url}");
  82.  
  83.                 // Get the product
  84.                 var useeer = await GetUserAsync(url.PathAndQuery);
  85.                 ShowUser(useeer);
  86.  
  87.                 //// Update the product
  88.                 //Console.WriteLine("Updating price...");
  89.                 //product.Price = 80;
  90.                 //await UpdateProductAsync(product);
  91.  
  92.                 //// Get the updated product
  93.                 //product = await GetProductAsync(url.PathAndQuery);
  94.                 //ShowProduct(product);
  95.  
  96.                 // Delete the product
  97.                 //var statusCode = await DeleteProductAsync(product.Id);
  98.                 //Console.WriteLine($"Deleted (HTTP Status = {(int)statusCode})");
  99.  
  100.             }
  101.             catch (Exception e)
  102.             {
  103.                 Console.WriteLine(e.Message);
  104.             }
  105.  
  106.             Console.ReadLine();
  107.         }
  108.  
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement