From c5879373c69c2180d9a4c4452580c108b96b505d Mon Sep 17 00:00:00 2001 From: Daniel Pichler Date: Wed, 28 Aug 2024 10:53:23 +0200 Subject: [PATCH] =?UTF-8?q?TryCatch=20hier=20unn=C3=B6tig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jugenddienst Stunden/Models/Auth.cs | 59 ++++++++++++----------------- 1 file changed, 25 insertions(+), 34 deletions(-) diff --git a/Jugenddienst Stunden/Models/Auth.cs b/Jugenddienst Stunden/Models/Auth.cs index f464a96..6ac76f7 100644 --- a/Jugenddienst Stunden/Models/Auth.cs +++ b/Jugenddienst Stunden/Models/Auth.cs @@ -11,47 +11,38 @@ namespace Jugenddienst_Stunden.Models { public Hours hours; public static async Task GetApiDataWithAuthAsync(string url, string token) { - try { - // Erstellen eines HttpClient-Objekts - using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(5) }) { - try { + // Erstellen eines HttpClient-Objekts + using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(5) }) { + - client.DefaultRequestHeaders.Add("Accept", "application/json"); + client.DefaultRequestHeaders.Add("Accept", "application/json"); - // Hinzufügen des Bearer-Tokens zum Authorization-Header - client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); + // Hinzufügen des Bearer-Tokens zum Authorization-Header + client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); - //Beim Debugging im Lokalen Netz mit meinem Smartphone kommt es hier zu - //system.net.webexception nachricht = socket closed - //Grund: Falscher DNS-Server liefert falsche Server-IP + //Beim Debugging im Lokalen Netz mit meinem Smartphone kommt es hier zu + //system.net.webexception nachricht = socket closed + //Grund: Falscher DNS-Server liefert falsche Server-IP - using (HttpResponseMessage HttpResponseMessage = await client.GetAsync(url).ConfigureAwait(false)) { - if (HttpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK) { - using (HttpContent HttpContent = HttpResponseMessage.Content) { - string responseData = await HttpContent.ReadAsStringAsync(); - return responseData; - } + using (HttpResponseMessage HttpResponseMessage = await client.GetAsync(url).ConfigureAwait(false)) { + if (HttpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK) { + using (HttpContent HttpContent = HttpResponseMessage.Content) { + string responseData = await HttpContent.ReadAsStringAsync(); + return responseData; } } - - // Senden der Anfrage und Abrufen der Antwort - //HttpResponseMessage response = await client.GetAsync(url); - - // Überprüfen, ob die Anfrage erfolgreich war - //response.EnsureSuccessStatusCode(); - - // Lesen und Rückgabe der Antwort als String - //string responseData = await response.Content.ReadAsStringAsync(); - //return responseData; - } catch (HttpRequestException e) { - // Fehlerbehandlung - Console.WriteLine($"An error occurred: {e.Message}"); - return null; } - } - } catch (Exception e) { - Console.WriteLine(e.Message); - return null; + + // Senden der Anfrage und Abrufen der Antwort + //HttpResponseMessage response = await client.GetAsync(url); + + // Überprüfen, ob die Anfrage erfolgreich war + //response.EnsureSuccessStatusCode(); + + // Lesen und Rückgabe der Antwort als String + //string responseData = await response.Content.ReadAsStringAsync(); + //return responseData; + } return null; }