Inquiridor
System.Net.Http.HttpRequestException: 'Ocorreu um erro ao enviar a solicitação.'

Pergunta
-
Olá estou com um problema que é o seguinte, criei uma API que faz consultas básicas num banco postgres. Na API está pegando os dados do banco e gravando tudo certinho. Todos os códigos abaixo possuem GET/PUT/POST/DELETE.
Estou começando agora no C# não sei o que preciso fazer, se precisa tratar esse SSL.
ClimaDB.cs
public static List<Clima> GetClima() { List<Clima> lista = new List<Clima>(); try { NpgsqlConnection conexao = Conexao.GetConexao(); string sql = "select * from clima"; NpgsqlCommand cmd = new NpgsqlCommand(sql, conexao); NpgsqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { int codigo = int.Parse(dr["clima_id"].ToString()); string temp = (string)dr["temp"]; string humd = (string)dr["humd"]; Clima clima = new Clima(); clima.clima_id = codigo; clima.temp = temp; clima.humd = humd; lista.Add(clima); } dr.Close(); Conexao.SetFechaConexao(conexao); } catch (NpgsqlException erro) { Console.WriteLine("Erro de sql. " + erro.Message); } return lista; }
ClimaController.cs
[Route("api/[controller]")] [ApiController] public class ClimaController : Controller { [AcceptVerbs("GET")] public List<Clima> GetClimaArduino() { return ClimaDB.GetClima(); }
Model: Clima.cs
public class Clima { public int clima_id { get; set; } public string temp { get; set; } public string humd { get; set; } }
Criei um client para pegar essa API
ClimaService.cs
public static async Task<List<Clima>> GetClima() { List<Clima> lista = new List<Clima>(); HttpClient client = new HttpClient(); client.BaseAddress = new Uri("https://localhost:44361/api/"); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.Timeout = new TimeSpan(0,0,30); HttpResponseMessage response = await client.GetAsync("clima"); lista = JsonConvert.DeserializeObject<List<Clima>>(await response.Content.ReadAsStringAsync()); return lista; }
FrmClima.cs
public FrmClimas() { InitializeComponent(); AtualizaTela(); } public async void AtualizaTela() { List<Clima> lista = await ClimaServices.GetClima(); GetGridClima.DataSource = lista; }
}
Quando executo o meu client ele retorna:
System.Reflection.TargetInvocationException
HResult=0x80131604
Message=Uma exceção foi acionada pelo destino de uma chamada.
Source=mscorlib
StackTrace:
em System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
em System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
em System.Delegate.DynamicInvokeImpl(Object[] args)
em System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
em System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
em System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
em System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
em System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
em System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
em System.Windows.Forms.Control.InvokeMarshaledCallbacks()
em System.Windows.Forms.Control.WndProc(Message& m)
em System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
em System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
em System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
em System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
em System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
em System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
em System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
em System.Windows.Forms.Application.RunDialog(Form form)
em System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
em System.Windows.Forms.Form.ShowDialog()
em WebClient.FrmPrincipal.Climas_Click(Object sender, EventArgs e) em D:\aula\ProjetoCSharp\WebClient\WebClient\FrmPrincipal.cs:linha 23
em System.Windows.Forms.Control.OnClick(EventArgs e)
em System.Windows.Forms.Button.OnClick(EventArgs e)
em System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
em System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
em System.Windows.Forms.Control.WndProc(Message& m)
em System.Windows.Forms.ButtonBase.WndProc(Message& m)
em System.Windows.Forms.Button.WndProc(Message& m)
em System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
em System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
em System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
em System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
em System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
em System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
em System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
em System.Windows.Forms.Application.Run(Form mainForm)
em WebClient.Program.Main() em D:\aula\ProjetoCSharp\WebClient\WebClient\Program.cs:linha 19
Exceção interna 1:
HttpRequestException: Ocorreu um erro ao enviar a solicitação.
Exceção interna 2:
WebException: A conexão subjacente estava fechada: Não foi possível estabelecer relação de confiança para o canal seguro de SSL/TLS.
Exceção interna 3:
AuthenticationException: O certificado remoto é inválido, de acordo com o procedimento de validação.
- Editado Andreclino domingo, 10 de novembro de 2019 18:24