Chuyển đổi HttpWebRequest sang HttpClient

This commit is contained in:
Le Chau 2020-06-25 09:22:24 +07:00
parent d359929042
commit a61195ca03

View File

@ -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<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;
}));
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<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)
{
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; }
}
}
}