Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net.Http.Formatting;
  7. using System.Net.Http;
  8. using WebAPI_1.Models;
  9. using System.IO;
  10. using System.Net.Mail;
  11. using System.Web.Script.Serialization;
  12.  
  13. namespace ConsoleApp1
  14. {
  15.     class Program
  16.     {
  17.         public static HttpClient client = new HttpClient();
  18.         static void Main(string[] args)
  19.         {
  20.             client.BaseAddress = new Uri("http://localhost:57652/api/");
  21.             client.DefaultRequestHeaders.Add("Accept", "application/json");
  22.  
  23.             Product produkt = new Product()
  24.             {
  25.                 Id = "2",
  26.                 Name = "sds"
  27.             };
  28.  
  29.             PostProducts(produkt);
  30.  
  31.             GetProducts();
  32.             Console.ReadLine();
  33.         }
  34.  
  35.         public static async void GetProducts()
  36.         {
  37.             var result = await client.GetAsync("Products");
  38.  
  39.             var strResult = await result.Content.ReadAsAsync<List<Product>>();
  40.             //var strResult = await result.Content.ReadAsStringAsync();
  41.             Console.WriteLine(strResult);
  42.         }
  43.  
  44.         public static async void PostProducts(Product product)
  45.         {
  46.             var obj = new Product() { Name = product.Name, Id = product.Id };
  47.             await client.PostAsJsonAsync("Products", obj);
  48.  
  49.             //await client.PostAsJsonAsync(client.BaseAddress, json);
  50.  
  51.         }
  52.     }
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement