TryCatch hier unnötig
This commit is contained in:
@@ -11,47 +11,38 @@ namespace Jugenddienst_Stunden.Models {
|
|||||||
public Hours hours;
|
public Hours hours;
|
||||||
|
|
||||||
public static async Task<string> GetApiDataWithAuthAsync(string url, string token) {
|
public static async Task<string> GetApiDataWithAuthAsync(string url, string token) {
|
||||||
try {
|
// Erstellen eines HttpClient-Objekts
|
||||||
// Erstellen eines HttpClient-Objekts
|
using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(5) }) {
|
||||||
using (HttpClient client = new HttpClient() { Timeout = TimeSpan.FromSeconds(5) }) {
|
|
||||||
try {
|
|
||||||
|
|
||||||
client.DefaultRequestHeaders.Add("Accept", "application/json");
|
client.DefaultRequestHeaders.Add("Accept", "application/json");
|
||||||
|
|
||||||
// Hinzufügen des Bearer-Tokens zum Authorization-Header
|
// Hinzufügen des Bearer-Tokens zum Authorization-Header
|
||||||
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
|
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
|
||||||
|
|
||||||
//Beim Debugging im Lokalen Netz mit meinem Smartphone kommt es hier zu
|
//Beim Debugging im Lokalen Netz mit meinem Smartphone kommt es hier zu
|
||||||
//system.net.webexception nachricht = socket closed
|
//system.net.webexception nachricht = socket closed
|
||||||
//Grund: Falscher DNS-Server liefert falsche Server-IP
|
//Grund: Falscher DNS-Server liefert falsche Server-IP
|
||||||
|
|
||||||
using (HttpResponseMessage HttpResponseMessage = await client.GetAsync(url).ConfigureAwait(false)) {
|
using (HttpResponseMessage HttpResponseMessage = await client.GetAsync(url).ConfigureAwait(false)) {
|
||||||
if (HttpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK) {
|
if (HttpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK) {
|
||||||
using (HttpContent HttpContent = HttpResponseMessage.Content) {
|
using (HttpContent HttpContent = HttpResponseMessage.Content) {
|
||||||
string responseData = await HttpContent.ReadAsStringAsync();
|
string responseData = await HttpContent.ReadAsStringAsync();
|
||||||
return responseData;
|
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) {
|
// Senden der Anfrage und Abrufen der Antwort
|
||||||
Console.WriteLine(e.Message);
|
//HttpResponseMessage response = await client.GetAsync(url);
|
||||||
return null;
|
|
||||||
|
// Ü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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user