Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Problem with HttpClient after updating Xamarin.iOS

$
0
0

Hi,

I'm currently investigating some strange behavior I see when using the System.Net.Http.HttpClient.

We have a REST API with NTLM Authentication enabled running on our server and our apps (iOS, Android, WP) get and POST data to this API. This solutions has been working fine for many months but last week we updated Xamarin Studio and Xamarion.iOS to the latest version and since then the POST (only the POSTs, GETs are working perfectly) stopped to work and the server returns 401 UNAUTHORIZED. This error is only happening on iOS, the Android and Windows Phone versions are still work perfectly with the same code... All of them use the same Service to communicate with the API.

It's super weird and it's driving us crazy. We tried to force old versions of the Microsoft.Net.Http, to use a package called ModernHttpClient... and nothing worked. We don't remember the old version of Xamarion.iOS but it was quite old... around 8.6, and now we are using the latest.

In the following lines you'll find the functions we use to perform the communications. First the GET function, that works perfectly and then the POST function that only doesn't work on iOS and was working perfectly on the previous version.

public async Task Get<T>(NetworkCredential credential, string url, Action<T> onSuccess, Action<PostErrorMessage> onError)
    {
        var httpClientHandler = new HttpClientHandler { Credentials = credential.GetCredential(new Uri(url), "NTLM") };

        using (var client = new HttpClient(httpClientHandler))
        {
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");

            try
            {
                var httpResponseMessage = await client.GetAsync(url);

                if (httpResponseMessage.StatusCode != HttpStatusCode.OK)
                {
                    onError(new PostErrorMessage(this, httpResponseMessage.ReasonPhrase, httpResponseMessage.StatusCode));
                }
                else
                {
                    var responseString = await httpResponseMessage.Content.ReadAsStringAsync();
                    var serializer = new JsonSerializer();
                    try
                    {
                        var json = serializer.Deserialize<T>(new JsonTextReader(new StringReader(responseString)));
                        onSuccess(json);
                    }
                    catch (Exception ex)
                    {
                        onError(new PostErrorMessage(this, ex.ToString(), httpResponseMessage.StatusCode));
                    }
                }
            }
            catch (Exception ex)
            {
                onError(new PostErrorMessage(this, ex != null ? ex.ToString() : string.Empty, HttpStatusCode.NotFound));
            }
        }
        System.Diagnostics.Debug.WriteLine(string.Format("DEBUG--------------------  seconds from {0}:[END Get]", DateTime.Now));
    }

public async Task Post<T>(NetworkCredential credential, string url, object data, Action<T> onSuccess, Action<PostErrorMessage> onError)
    {
        var httpClientHandler = new HttpClientHandler { Credentials = credential.GetCredential(new Uri(url), "NTLM") };

        using (var client = new HttpClient(httpClientHandler))
        {
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");

            var httpResponseMessage = await client.PostAsync(
                    new Uri(url),
                    new StringContent(
                        JsonConvert.SerializeObject(data, new JsonSerializerSettings()),
                        System.Text.Encoding.UTF8,
                        "application/json"));

            if (httpResponseMessage.StatusCode != HttpStatusCode.Created &&
                httpResponseMessage.StatusCode != HttpStatusCode.OK)
            {
                onError(new PostErrorMessage(this, httpResponseMessage.ReasonPhrase, httpResponseMessage.StatusCode));
            }
            else
            {
                var responseString = await httpResponseMessage.Content.ReadAsStringAsync();
                var serializer = new JsonSerializer();
                var json = serializer.Deserialize<T>(new JsonTextReader(new StringReader(responseString)));
                onSuccess(json);
            }
        }
    }

Any help or any clue will be more than welcome :)

Many thanks in advance!

Jordi


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>