Xamarin UWP HTTPS REST call returns gibberish

GianniAracoGianniAraco USMember
edited September 2017 in Cross Platform with Xamarin

It seems I've hit a brick wall here and need new pairs of eyes to see the issue from different angles.

Context

I have a Xamarin.Forms PCL application for iOS, Android and UWP. I need to call REST services; some are plain HTTP and other are HTTPS.
I'm using:
1. Xamarin.Forms 2.3.4.270
2. .NET Framework 4.5
3. the package Microsoft.Net.Http v2.2.29

Issue

For iOS and Android everything works perfectly fine. For UWP, HTTP calls work fine, certain HTTPS calls, instead of the expected JSON, return gibberish. This is a sample:

"\u001f�\b\0\0\0\0\0\0��V\nK-�����W�244��Q\n-N-�K�MU�RO�+����I�ϪJ,(P�Q\n��\u0001I8��f�)�\u0002\02d+�>\0\0\0"

Tested HTTPS on server with only SSL2.0 activated and it's working as expected. On the live server, which has:
1. PCT1.0, SSL2.0, SSL3.0 -> disabled
2. TLS1.0, TLS1.1 and TLS1.2
I get that encrypted/encoded string.
I don't know which one the call is using, I guess it should be TLS1.2.
I also tried to decode it using UTF8, to no avail.

Has anyone encountered a similar issue?

The HttpClient is defined as follows in the PCL's App.xaml.cs file:

public static HttpClient HttpClient
{
    get
    {
        if (_httpClient == null)
        {
            HttpClientHandler handler = new HttpClientHandler()
            {
                AllowAutoRedirect = false
            };
            _httpClient = new HttpClient(handler);
        }

        return _httpClient;
    }
}

And that's the method that does the REST call, also in the PCL:

protected async Task<TResult> RequestAsync<TParam, TResult>(string uri, bool isPost, TParam param, AuthenticationHeaderValue authorization)
    where TResult : IClientApiModel, new()
{
    HttpRequestMessage request = null;
    HttpResponseMessage response = null;
    try
    {
        request = new HttpRequestMessage(isPost ? HttpMethod.Post : HttpMethod.Get, uri);
        request.Headers.Authorization = authorization;

        if (param != null)
        {
            var data = new StringContent(JsonConvert.SerializeObject(param));
            request.Content = data;
            request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
        }

        response = await App.HttpClient.SendAsync(request);
        if (response.StatusCode == HttpStatusCode.Redirect || response.StatusCode == HttpStatusCode.MovedPermanently)
        {
            Uri redirectUri = response.Headers.Location;
            request.Dispose();
            request = new HttpRequestMessage(isPost ? HttpMethod.Post : HttpMethod.Get, redirectUri);
            response = await App.HttpClient.SendAsync(request);
        }

        if (response.StatusCode != HttpStatusCode.OK)
        {
            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                return new TResult() { HasErrors = true, HasNoConnection = true };
            }
            return new TResult() { HasErrors = true };
        }
        var content = await response.Content.ReadAsStringAsync();

        TResult model = content.IsJson() ?
            JsonConvert.DeserializeObject<TResult>(content) :
            new TResult() { HasErrors = true };

        return model;
    }
    catch (WebException weberror)
    {
        throw weberror;
    }
    catch
    {
        var def = new TResult() { HasErrors = true };
        return def;
    }
    finally
    {
        if (request != null)
        {
            request.Dispose();
        }
        if (response != null)
        {
            response.Dispose();
        }
    }
}
Sign In or Register to comment.