langbung01 icon

request http

langbung01 | PRO | 03/04/19 03:12:58 AM UTC | 0 ⭐ | 519 👁️ | Never ⏰ | []
C# |

1.21 KB

|

None

|

0 👍

/

0 👎

using System.Net.Http;
namespace Smartcard.Controller
{
    public class LoginController
    {
        private static readonly HttpClient client = new HttpClient();
 
        //request get http
        public async void requestGetHttp()
        {
            var response = await client.GetAsync("https://jsonplaceholder.typicode.com/posts");
            var responseString = await response.Content.ReadAsStringAsync();
            Console.WriteLine(responseString);
        }
 
        //request post http
        public async void requestPostHttp()
        {
            var values = new Dictionary<string, string>
            {
               { "thing1", "hello" },
               { "thing2", "world" }
            };
 
            var content = new FormUrlEncodedContent(values);
 
            try
            {
                var response = await client.PostAsync("https://jsonplaceholder.typicode.com/posts", content);
                var responseString = await response.Content.ReadAsStringAsync();
                Console.WriteLine(responseString);
            } catch (HttpRequestException e) {
                MessageBox.Show("No internet","แจ้งเตือน");
            }
        }
    }
}

Comments