From a61195ca03b0bf1ea7c5d5ba3f071cbf461d348b Mon Sep 17 00:00:00 2001 From: Le Chau Date: Thu, 25 Jun 2020 09:22:24 +0700 Subject: [PATCH] =?UTF-8?q?Chuy=E1=BB=83n=20=C4=91=E1=BB=95i=20HttpWebRequ?= =?UTF-8?q?est=20sang=20HttpClient?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AIParkingApplication/Statistic.cs | 83 +++++++++++++++---------------- 1 file changed, 39 insertions(+), 44 deletions(-) diff --git a/AIParkingApplication/Statistic.cs b/AIParkingApplication/Statistic.cs index 79cd852..ffaac09 100644 --- a/AIParkingApplication/Statistic.cs +++ b/AIParkingApplication/Statistic.cs @@ -6,6 +6,8 @@ using System.Net; using System.Web.Script.Serialization; using System.Diagnostics.SymbolStore; using System.Threading; +using Newtonsoft.Json; +using System.Net.Http; namespace AIParkingApplication { @@ -26,62 +28,48 @@ namespace AIParkingApplication InitializeComponent(); Thread thrStatistics = new Thread(new ThreadStart(GetStatistic)); thrStatistics.Start(); - } private void GetStatistic() { while (true) { - string strGETRequest = GETDataFromServer("192.168.1.123", "80", "/api/statistics"); - JavaScriptSerializer js = new JavaScriptSerializer(); - var obj = js.Deserialize(strGETRequest); - Vehicle.NumberOfCar = obj["oto"]; - Vehicle.NumberOfMoto = obj["moto"]; - Vehicle.TotalIn = obj["total_in"]; - Vehicle.TotelOut = obj["total_out"]; - lblSoLuongXeMay?.Invoke(new Action(() => - { - lblSoLuongXeMay.Text = Vehicle.NumberOfMoto.ToString(); - })); - lblSoLuongOto?.Invoke(new Action(() => - { - lblSoLuongOto.Text = Vehicle.NumberOfCar.ToString(); - })); - lblVehicleTotalIn?.Invoke(new Action(() => - { - lblVehicleTotalIn.Text = Vehicle.TotalIn; - })); - lblVehicleTotalOut?.Invoke(new Action(() => - { - lblVehicleTotalOut.Text = Vehicle.TotelOut; - })); + GETDataFromServer(); Thread.Sleep(1000); } } - private string GETDataFromServer(string serverUrl, string apiPort, string apiPath) + private async void GETDataFromServer() { - string responseString = ""; - string requestUriString = string.Format(AIPARKING_CLIENT_HTTP_API_REQUEST, serverUrl, apiPort, apiPath); - var request = (HttpWebRequest)WebRequest.Create(requestUriString); - string postData = $""; - byte[] data = Encoding.ASCII.GetBytes(postData); - - request.Method = "GET"; - request.ContentType = "application/x-www-form-urlencoded"; - request.ContentLength = data.Length; - try { - var response = (HttpWebResponse)request.GetResponse(); - responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); - Console.WriteLine(responseString); + using (var client = new HttpClient { BaseAddress = new Uri("http://192.168.1.123:80/"), Timeout = TimeSpan.FromMilliseconds(5000) }) + { + HttpResponseMessage response = await client.GetAsync("/api/statistics"); + response.EnsureSuccessStatusCode(); + var vehicle = await response.Content.ReadAsAsync(); + lblSoLuongXeMay?.Invoke(new Action(() => + { + lblSoLuongXeMay.Text = vehicle.NumberOfMoto.ToString(); + })); + lblSoLuongOto?.Invoke(new Action(() => + { + lblSoLuongOto.Text = vehicle.NumberOfCar.ToString(); + })); + lblVehicleTotalIn?.Invoke(new Action(() => + { + lblVehicleTotalIn.Text = vehicle.TotalIn; + })); + lblVehicleTotalOut?.Invoke(new Action(() => + { + lblVehicleTotalOut.Text = vehicle.TotelOut; + })); + } } catch (Exception ex) { + Console.WriteLine($"SendEngineRequest : {ex.Message}"); } - return responseString; } private void Statistic_Load(object sender, EventArgs e) @@ -107,12 +95,19 @@ namespace AIParkingApplication tlpStatisticTable.Controls.Add(lblSoLuongOto, 1, 2); } - static class Vehicle + public class Vehicle { - public static int NumberOfCar { get; set; } - public static int NumberOfMoto { get; set; } - public static string TotalIn { get; set; } - public static string TotelOut { get; set; } + [JsonProperty("oto")] + public int NumberOfCar { get; set; } + + [JsonProperty("moto")] + public int NumberOfMoto { get; set; } + + [JsonProperty("total_in")] + public string TotalIn { get; set; } + + [JsonProperty("total_out")] + public string TotelOut { get; set; } } } }