Handle HttpClient errors
This commit is contained in:
parent
b18e573da4
commit
ded0520756
@ -2,6 +2,7 @@ using System.Net.Http;
|
|||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using System.Windows.Forms;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace RhSolutions.Services;
|
namespace RhSolutions.Services;
|
||||||
@ -36,39 +37,46 @@ public class YandexOcrClient : IOcrClient
|
|||||||
_httpClient.DefaultRequestHeaders.Add("x-data-logging-enable", "true");
|
_httpClient.DefaultRequestHeaders.Add("x-data-logging-enable", "true");
|
||||||
|
|
||||||
using HttpResponseMessage response = await _httpClient.PostAsync("recognizeText", jsonContent);
|
using HttpResponseMessage response = await _httpClient.PostAsync("recognizeText", jsonContent);
|
||||||
response.EnsureSuccessStatusCode();
|
if (response.IsSuccessStatusCode)
|
||||||
|
|
||||||
string jsonResponse = await response.Content.ReadAsStringAsync();
|
|
||||||
OcrResponse deserialized = JsonConvert.DeserializeObject<OcrResponse>(jsonResponse);
|
|
||||||
|
|
||||||
if (deserialized != null)
|
|
||||||
{
|
{
|
||||||
var tables = deserialized?.Result?.TextAnnotation?.Tables ?? Enumerable.Empty<Table>();
|
string jsonResponse = await response.Content.ReadAsStringAsync();
|
||||||
if (tables.Any())
|
OcrResponse deserialized = JsonConvert.DeserializeObject<OcrResponse>(jsonResponse);
|
||||||
{
|
|
||||||
List<object[,]> result = new();
|
|
||||||
foreach (var table in tables)
|
|
||||||
{
|
|
||||||
if (table.Cells == null || table.Cells.Count == 0)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
int columnCount = int.Parse(table.ColumnCount);
|
|
||||||
int rowCount = int.Parse(table.RowCount);
|
|
||||||
object[,] cells = new object[rowCount, columnCount];
|
|
||||||
|
|
||||||
foreach (Cell cell in table.Cells)
|
if (deserialized != null)
|
||||||
|
{
|
||||||
|
var tables = deserialized?.Result?.TextAnnotation?.Tables ?? Enumerable.Empty<Table>();
|
||||||
|
if (tables.Any())
|
||||||
|
{
|
||||||
|
List<object[,]> result = new();
|
||||||
|
foreach (var table in tables)
|
||||||
{
|
{
|
||||||
int rowIndex = int.Parse(cell.RowIndex);
|
if (table.Cells == null || table.Cells.Count == 0)
|
||||||
int columnIndex = int.Parse(cell.ColumnIndex);
|
{
|
||||||
cells[rowIndex, columnIndex] = double.TryParse(cell.Text, out double v) ?
|
continue;
|
||||||
v : cell.Text ?? string.Empty;
|
}
|
||||||
|
int columnCount = int.Parse(table.ColumnCount);
|
||||||
|
int rowCount = int.Parse(table.RowCount);
|
||||||
|
object[,] cells = new object[rowCount, columnCount];
|
||||||
|
|
||||||
|
foreach (Cell cell in table.Cells)
|
||||||
|
{
|
||||||
|
int rowIndex = int.Parse(cell.RowIndex);
|
||||||
|
int columnIndex = int.Parse(cell.ColumnIndex);
|
||||||
|
cells[rowIndex, columnIndex] = double.TryParse(cell.Text, out double v) ?
|
||||||
|
v : cell.Text ?? string.Empty;
|
||||||
|
}
|
||||||
|
result.Add(cells);
|
||||||
}
|
}
|
||||||
result.Add(cells);
|
return result;
|
||||||
}
|
}
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string content = await response.Content.ReadAsStringAsync();
|
||||||
|
MessageBox.Show($"{response.StatusCode}: {content}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
|
}
|
||||||
|
|
||||||
return Enumerable.Empty<object[,]>();
|
return Enumerable.Empty<object[,]>();
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user