StringContent is empty on Xamarin.Android

We have an Azure WebApi service that can sometimes return a BadRequest, for example,

return BadRequest("{\"errors\":[{\"message\":\"MyBuddy not found!\",\"code\":9}]}"); 

The problem is that on Xamarin.Android, using System.Net.Http.HttpClient, the content of the response we receive is empty.

This is our code:

 private static async Task<int> ReadErrorCodeAsync(HttpResponseMessage response) { var x = response.ReasonPhrase; var errorSerialized = await response.Content.ReadAsStringAsync(); var error = JObject.Parse(errorSerialized); return ((JArray)error["errors"])[0]["code"].Value<int>(); } 

But errorSerialized is always an empty string. We also use the Swagger user interface, and there is response content there, for example:

 { "errors": [ { "message": "MyBuddy not found!", "code": 9 } ] } 

Is it normal that the content of the BadRequest response is empty on Xamarin.Android? Is there anything I can do, for example. configure HttpClient differently to get StringContent?

EDIT: we are using AndroidClientHandler . To do this, we added a text file to the Android project, installed its BuildAction on AndroidResource and its contents on XA_HTTP_CLIENT_HANDLER_TYPE=Xamarin.Android.Net.AndroidClientHandler

+6
source share
1 answer

This is a bug in the AndroidClientHandler . It reads the error incorrectly. (see https://github.com/xamarin/xamarin-android/blob/master/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs#L308 )

I fixed it. Let them hope they condense it quickly :) https://github.com/xamarin/xamarin-android/pull/180

If you need to use it before Xamarin merged and released it, you should:

  • copy the folder to any folder in the Android project.
  • change the namespace in all files to Xamarin.Android.Net.Fix
  • Delete or comment out all Logger lines that cause compilation errors.
  • change the contents of the environment file to XA_HTTP_CLIENT_HANDLER_TYPE=Xamarin.Android.Net.Fix.AndroidClientHandler,MyApp.Android (replace MyApp.Android with the build name of your Android application)
+3
source

All Articles