From b41d272a7af790a0a5c27199dd869ac89c9f48d9 Mon Sep 17 00:00:00 2001 From: Adriano Serighelli Date: Mon, 5 Jan 2026 16:55:36 -0300 Subject: [PATCH] =?UTF-8?q?Melhora=20requisi=C3=A7=C3=A3o=20HTTP=20no=20Ca?= =?UTF-8?q?lendarioService?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Utiliza SocketsHttpHandler para descompressão automática (GZip, Deflate, Brotli) e configura HttpRequestMessage com headers detalhados (Host, User-Agent, Accept, Accept-Encoding, Connection) para simular navegador e garantir compatibilidade com o servidor da CCEE. Troca PostAsync por SendAsync. Processamento do JSON permanece inalterado. --- Infrastructure/CalendarioService.cs | 39 +++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/Infrastructure/CalendarioService.cs b/Infrastructure/CalendarioService.cs index b340a13..1405724 100644 --- a/Infrastructure/CalendarioService.cs +++ b/Infrastructure/CalendarioService.cs @@ -1,6 +1,9 @@ -using System.Text.Json; +using System.Text.Json; using System.Globalization; using Application; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; namespace Infrastructure { @@ -32,9 +35,31 @@ namespace Infrastructure try { - using var client = new HttpClient(); + using var handler = new SocketsHttpHandler + { + // Adiciona Accept-Encoding automaticamente e faz a descompressão transparente + AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate | DecompressionMethods.Brotli + }; + using var client = new HttpClient(handler); using var content = new FormUrlEncodedContent(values); - var resp = await client.PostAsync(Url, content, ct); + + using var request = new HttpRequestMessage(HttpMethod.Post, Url) + { + Content = content + }; + + // Headers solicitados + request.Headers.Host = "www.ccee.org.br"; + request.Headers.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"); + request.Headers.Accept.Clear(); + request.Headers.Accept.ParseAdd("*/*"); + request.Headers.AcceptEncoding.Clear(); + request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip")); + request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate")); + request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("br")); + request.Headers.Connection.Add("keep-alive"); + + var resp = await client.SendAsync(request, ct); var resposta = await resp.Content.ReadAsStringAsync(ct); var alvo = $"Data limite para coleta diária dos dados de medição no SCDE"; @@ -42,14 +67,14 @@ namespace Infrastructure try { using var doc = JsonDocument.Parse(resposta); - + double? encontrado = null; double? startTimeDay = null; void SearchJson(JsonElement el) { if (encontrado != null) return; - + if (el.ValueKind == JsonValueKind.Object) { // Verifica se alguma propriedade contém o texto alvo @@ -69,7 +94,7 @@ namespace Infrastructure } } } - + foreach (var prop in el.EnumerateObject()) SearchJson(prop.Value); } @@ -79,7 +104,7 @@ namespace Infrastructure SearchJson(item); } } - + SearchJson(doc.RootElement); if (encontrado != null) return encontrado; }