Chuyển đổi HttpWebRequest sang HttpClient
This commit is contained in:
parent
d359929042
commit
a61195ca03
|
@ -6,6 +6,8 @@ using System.Net;
|
||||||
using System.Web.Script.Serialization;
|
using System.Web.Script.Serialization;
|
||||||
using System.Diagnostics.SymbolStore;
|
using System.Diagnostics.SymbolStore;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using System.Net.Http;
|
||||||
|
|
||||||
namespace AIParkingApplication
|
namespace AIParkingApplication
|
||||||
{
|
{
|
||||||
|
@ -26,62 +28,48 @@ namespace AIParkingApplication
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
Thread thrStatistics = new Thread(new ThreadStart(GetStatistic));
|
Thread thrStatistics = new Thread(new ThreadStart(GetStatistic));
|
||||||
thrStatistics.Start();
|
thrStatistics.Start();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GetStatistic()
|
private void GetStatistic()
|
||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
string strGETRequest = GETDataFromServer("192.168.1.123", "80", "/api/statistics");
|
GETDataFromServer();
|
||||||
JavaScriptSerializer js = new JavaScriptSerializer();
|
|
||||||
var obj = js.Deserialize<dynamic>(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;
|
|
||||||
}));
|
|
||||||
Thread.Sleep(1000);
|
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
|
try
|
||||||
{
|
{
|
||||||
var response = (HttpWebResponse)request.GetResponse();
|
using (var client = new HttpClient { BaseAddress = new Uri("http://192.168.1.123:80/"), Timeout = TimeSpan.FromMilliseconds(5000) })
|
||||||
responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
|
{
|
||||||
Console.WriteLine(responseString);
|
HttpResponseMessage response = await client.GetAsync("/api/statistics");
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
var vehicle = await response.Content.ReadAsAsync<Vehicle>();
|
||||||
|
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)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
Console.WriteLine($"SendEngineRequest : {ex.Message}");
|
||||||
}
|
}
|
||||||
return responseString;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Statistic_Load(object sender, EventArgs e)
|
private void Statistic_Load(object sender, EventArgs e)
|
||||||
|
@ -107,12 +95,19 @@ namespace AIParkingApplication
|
||||||
tlpStatisticTable.Controls.Add(lblSoLuongOto, 1, 2);
|
tlpStatisticTable.Controls.Add(lblSoLuongOto, 1, 2);
|
||||||
|
|
||||||
}
|
}
|
||||||
static class Vehicle
|
public class Vehicle
|
||||||
{
|
{
|
||||||
public static int NumberOfCar { get; set; }
|
[JsonProperty("oto")]
|
||||||
public static int NumberOfMoto { get; set; }
|
public int NumberOfCar { get; set; }
|
||||||
public static string TotalIn { get; set; }
|
|
||||||
public static string TotelOut { 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; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user