Search code examples
httpclientmaui

Detection of exception type in HttpClient sendAsync method


I'm trying to determine what exception I got, but all exceptions are of type System.AggregateException and unimportant - SSL validation failed and host is unreachable.

HttpClient client = new HttpClient();
HttpRequestMessage httpRequestMessage = new HttpRequestMessage {
    Method = HttpMethod.Get,
    RequestUri = new Uri("https://mydomain.ru"), 
};

try {
    HttpResponseMessage httpResponseMessage = client.SendAsync(httpRequestMessage).Result;

    return ((int) httpResponseMessage.StatusCode, httpResponseMessage.Content.ReadAsStringAsync().Result);
} catch (System.Exception e) {
   // e.GetType() always return `System.AggregateException`
   return (500, $"{e.Message}; Error type: `{e.GetType().ToString()}`); 
}

Exist some way for detect what is the error?


Solution

  • HttpClient package work with platform implementation it's produce specific exception on error on different platforms.

    For adequate processed exception need to use compile-condition blocks for needed platform (in this code is iOS and Android).
    On Android AggregateException has in InnerException a differents exceptions, iOS use code property for indicate exception types

    HttpClient client = new HttpClient();
    HttpRequestMessage httpRequestMessage = new HttpRequestMessage {
        Method = HttpMethod.Get,
        RequestUri = new Uri("https://mydomain.ru"), 
    };
    
    try {
        HttpResponseMessage httpResponseMessage = client.SendAsync(httpRequestMessage).Result;
    
        return ((int) httpResponseMessage.StatusCode, httpResponseMessage.Content.ReadAsStringAsync().Result);
    } catch (System.Exception e) {
        String errorMessage = "UNKNOWN CRITICAL ERROR";
        if (e is not System.AggregateException) {
            errorMessage = $"{e.Message}; Error type: `{e.GetType().ToString()}`";
        } else {
            ((AggregateException) e).Handle((x) => {
    #if ANDROID
                if (e.InnerException?.InnerException is Javax.Net.Ssl.SSLHandshakeException) {
                    errorMessage = "SSL validation error";
                } else if (e.InnerException?.InnerException is Java.Net.ConnectException) {
                    errorMessage = "Unreachable host, check Internet connection";
                }
    #elif IOS
                if (e.InnerException?.InnerException is Foundation.NSErrorException) {
                    if (((Foundation.NSErrorException) e.InnerException?.InnerException)?.Code == -1202) {
                        errorMessage = "SSL validation error";
                    } else if (((Foundation.NSErrorException) e.InnerException?.InnerException)?.Code == -1009) {
                        errorMessage = "Unreachable host, check Internet connection";
                    }
                }
    #endif
    
                    return true;
            });
        }
    
    
       // e.GetType() always return `System.AggregateException`
       return (500, errorMessage); 
    }