C # HttpClient throws exception without Sucess status codes
I have a very strange problem. When I request a web page that returns a status code that is not a 2 ** status code than HttpClient throws ecxeption.
Implementation:
try {
//post-anfrage an webservice starten
var request = await httpClient.PostAsync(SettingsPage.WebServiceURL + site,
content);
if (!request.IsSuccessStatusCode) {
if (request.StatusCode == System.Net.HttpStatusCode.Unauthorized) {
await User.Instance.Logout();
}
var message = await request.Content.ReadAsStringAsync();
RaiseErrorMessage(null, message);
}
SaveCookies(request);
timer.Change(PING_INTERVAL, PING_INTERVAL);
return request;
}
catch (TaskCanceledException e) {
RaiseConnectionError(null, "Verbindung zu Webservice fehlgeschlagen!");
return null;
}
catch (Exception ex) {
RaiseConnectionError(null, "Es ist etwas schief gelaufen..." + System.Environment.NewLine + "Bitte Support Kontaktieren!");
Debug.WriteLine("AppWarehouse.pro: " + ex.Message);
Debug.WriteLine("AppWarehouse.pro: " + ex.StackTrace);
return null;
}
}
I don't understand why he does what he does. There is no reason for me to do this. I would be happy to fix or help fix this.
+3
source to share