HttpClient behaves strangely after updating Xamarin.iOS

In our project, we are using a custom web API client as a nuget package.

using (var response = await _httpClient.SendAsync(request))
{
    try
{
        response.EnsureSuccessStatusCode();
    }
    catch (Exception exception)
    {
        throw new CustomException(exception);
    }
}

(These custom exceptions are handled by the app to show different error messages)

For requests with invalid data/syntax, the HttpResponse sent by the API includes a 400 status code with a custom reason phrase, and the response's reason phrase should be: 400 (Failed: CustomErrorMessage).

This was the 400 error message for both Android & iOS platforms for Bad Requests until we have updated the Xamarin.iOS and Mono versions. After the update, the iOS response message is showing 400 (Bad Request) while android still gets the same error message as the previous one: 400 (Failed: CustomErrorMessage).

Android stack trace:
{System.Net.Http.HttpRequestException: Response status code does not indicate success: 400 (Failed: CustomErrorMessage).
at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode ()}

iOS stack trace:
{System.Net.Http.HttpRequestException: 400 (Bad Request)
at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode () [0x0000a] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/mcs/class/System.Net.Http/System.Net.Http/HttpResponseMessage.cs:123 }

The HttpClient implementation for iOS was selected as NSUrlSession (from iOS build options). After reading this (https://docs.microsoft.com/en-us/xamarin/cross-platform/macios/http-stack#selecting-an-httpclient-stack) we changed the HttpClient implementation to Managed, and we were able to get the errors as expected.

How changing NSUrlSession to Managed HttpClient change the HttpResponse message and its content? And, is it okay to use the Managed HttpClient implementation rather than a native one?

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    As it mentioned in the article .

    In order to ensure your apps continue to work with these servers and services, you should update your Xamarin projects with the NSUrlSession setting shown below, then re-build and re-deploy your apps to your users.

    Consider raising issue :https://github.com/xamarin/Xamarin.Forms/issues .

  • tharindu_mptharindu_mp Member ✭✭

    The project is currently using NSUrlSession settings.
    @ColeX could you explain the difference between CFNetwork and NSUrlSession? :)

  • ColeXColeX Member, Xamarin Team Xamurai

    @tharindu_mp said:
    The project is currently using NSUrlSession settings.
    @ColeX could you explain the difference between CFNetwork and NSUrlSession? :)

    Maybe you should ask what's the difference between NSURLConnection and NSUrlSession , since CFNetwork is a lower-level C class that is basically wrapped by the NSURLConnection class . And check What is the biggest difference between NSURLConnection and NSURLSession .

    In brief , CFNetwork is older api ,NSUrlSession is more advanced , we used NSUrlSession now.

Sign In or Register to comment.