Util - use static HttpClient httpClientEngine as field.

This commit is contained in:
DucDangAnh 2020-06-29 15:19:36 +07:00
parent 148896423f
commit cc92f80a45

View File

@ -10,6 +10,8 @@ namespace AIParkingApplication
{ {
public static class Util public static class Util
{ {
private static HttpClient httpClientEngine = new HttpClient { BaseAddress = new Uri("http://localhost:8080/"), Timeout = TimeSpan.FromMilliseconds(5000) };
public static bool IsPingable(string destinationIPAddress, out long pingTime, int timeOut = UtilConstant.PING_TIMEOUT_MS) public static bool IsPingable(string destinationIPAddress, out long pingTime, int timeOut = UtilConstant.PING_TIMEOUT_MS)
{ {
var pingTask = Task.Factory.StartNew(async () => var pingTask = Task.Factory.StartNew(async () =>
@ -54,8 +56,6 @@ namespace AIParkingApplication
{ {
string plateImageBase64 = Convert.ToBase64String(plateImage.ToBytes()); string plateImageBase64 = Convert.ToBase64String(plateImage.ToBytes());
try try
{
using (var client = new HttpClient { BaseAddress = new Uri("http://localhost:8080/"), Timeout = TimeSpan.FromMilliseconds(5000) })
{ {
var request = new PlateRequestEngineModel var request = new PlateRequestEngineModel
{ {
@ -63,12 +63,11 @@ namespace AIParkingApplication
Mode = plateType == PlateType.Square ? "square" : "long", Mode = plateType == PlateType.Square ? "square" : "long",
Display = "full" Display = "full"
}; };
HttpResponseMessage response = await client.PostAsJsonAsync("/get-from-frame", request); HttpResponseMessage response = await httpClientEngine.PostAsJsonAsync("/get-from-frame", request);
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
var ocrResult = await response.Content.ReadAsAsync<OcrResult>(); var ocrResult = await response.Content.ReadAsAsync<OcrResult>();
return ocrResult; return ocrResult;
} }
}
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"SendEngineRequest : {ex.Message}"); Console.WriteLine($"SendEngineRequest : {ex.Message}");