Guest User

Untitled

a guest
Jan 22nd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. private static void HttpGetAll()
  2. {
  3. var client = new HttpClient();
  4. HttpResponseMessage response = client.Get(CONTACTS_URI);
  5.  
  6. Console.WriteLine("GET: {0} RESPONSE: {1} \n{2}\n",
  7. CONTACTS_URI,
  8. response.StatusCode,
  9. response.Content.ReadAsString());
  10.  
  11. //var contacts = response.Content.ReadAs<Contact[]>();
  12. //Console.WriteLine("STATICALLY TYPE CONTACTS COUNT: {0}\n", contacts.Length);
  13. }
  14.  
  15. private static void HttpPost()
  16. {
  17. var client = new HttpClient();
  18. client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(MediaTypes.JSON));
  19. HttpContent jsonContactNew = new StringContent("{\"Id\":0,\"Name\":\"Edward\"}", Encoding.UTF8, MediaTypes.JSON);
  20. HttpResponseMessage response = client.Post(CONTACTS_URI, jsonContactNew);
  21.  
  22. Console.WriteLine("POST (INSERT CONTACT): {0} RESPONSE: {1} \n{2}\n",
  23. response.Headers.Location,
  24. response.StatusCode,
  25. response.Content.ReadAsString());
  26. }
  27.  
  28. private static void HttpPut()
  29. {
  30. var client = new HttpClient();
  31. client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(MediaTypes.JSON));
  32. HttpContent jsonContactUpdated = new StringContent("{\"Id\":2,\"Name\":\"Ziggy\"}", Encoding.UTF8, MediaTypes.JSON);
  33. HttpResponseMessage response = client.Put(CONTACTS_URI, jsonContactUpdated);
  34.  
  35. Console.WriteLine("POST (UPDATE CONTACT ID 2): {0} RESPONSE: {1}\n",
  36. CONTACTS_URI,
  37. response.StatusCode);
  38. }
  39.  
  40. private static void HttpDelete()
  41. {
  42. var client = new HttpClient();
  43. const string uri = CONTACTS_URI + "/3";
  44. HttpResponseMessage response = client.Delete(uri);
  45.  
  46. Console.WriteLine("DELETE (CONTACT ID 3): {0} RESPONSE: {1} \n",
  47. uri,
  48. response.StatusCode);
  49. }
  50.  
  51. private static void HttpGet()
  52. {
  53. var client = new HttpClient();
  54. const string uri = CONTACTS_URI + "/1";
  55. HttpResponseMessage response = client.Get(uri);
  56.  
  57. Console.WriteLine("GET: {0} RESPONSE: {1} \n{2}\n",
  58. uri,
  59. response.StatusCode,
  60. response.Content.ReadAsString());
  61.  
  62. //var contact = response.Content.ReadAs<Contact>();
  63. //Console.WriteLine("STATICALLY TYPE CONTACT: {0}\n", contact);
  64. }
Add Comment
Please, Sign In to add comment