langbung01

request http

Mar 3rd, 2019
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1. using System.Net.Http;
  2. namespace Smartcard.Controller
  3. {
  4.     public class LoginController
  5.     {
  6.         private static readonly HttpClient client = new HttpClient();
  7.  
  8.         //request get http
  9.         public async void requestGetHttp()
  10.         {
  11.             var response = await client.GetAsync("https://jsonplaceholder.typicode.com/posts");
  12.             var responseString = await response.Content.ReadAsStringAsync();
  13.             Console.WriteLine(responseString);
  14.         }
  15.  
  16.         //request post http
  17.         public async void requestPostHttp()
  18.         {
  19.             var values = new Dictionary<string, string>
  20.             {
  21.                { "thing1", "hello" },
  22.                { "thing2", "world" }
  23.             };
  24.  
  25.             var content = new FormUrlEncodedContent(values);
  26.  
  27.             try
  28.             {
  29.                 var response = await client.PostAsync("https://jsonplaceholder.typicode.com/posts", content);
  30.                 var responseString = await response.Content.ReadAsStringAsync();
  31.                 Console.WriteLine(responseString);
  32.             } catch (HttpRequestException e) {
  33.                 MessageBox.Show("No internet","แจ้งเตือน");
  34.             }
  35.         }
  36.     }
  37. }
Add Comment
Please, Sign In to add comment