Alex
Alex

Reputation: 459

MAUI decode Base64 webservice responses fails with error, and yet the string seems to be valid

I am calling in Net Maui a webservice, like this:

internal static bool GetTypeConfigurations(string smartRequestUrl, string serviceToken, out DataTable? dt)
        {
            bool okay = true;
            dt = null;

            try
            {
                string? response = smartRequestUrl
                    .AppendPathSegment("MyRequest")
                    .AppendPathSegment("GetTypeConfigurations")
                    .WithTimeout(TimeSpan.FromSeconds(15))
                    .WithOAuthBearerToken(serviceToken)
                    .GetStringAsync()
                    .Result.Trim();

                dt = Functions.ConvertFromBase64String<DataTable>(response);
                okay = (dt != null);
            }
            catch (Exception ex)
            {
                okay = false;
                ErrorHandling.HandleError(ex);
            }

            return okay;
        }

This is a very simple function (I am using Flurl). Then I have

internal static T? ConvertFromBase64String<T>(string content) where T : class
        {
            byte[]? bytes = null;

            try
            {
                if (content == null)
                    return null;

                bytes = Convert.FromBase64String(content);
            }
            catch (Exception ex)
            {
                ErrorHandling.HandleError(ex);
            }

            return (T)DecompressNone(bytes);
        }

        private static byte[]? CompressNone(object obj)
        {
            byte[]? serializedObj = null;

            try
            {
                using MemoryStream ms = new MemoryStream();
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                binaryFormatter.Serialize(ms, obj);

                WSEBugWorkAroundSerialization(ms);

                serializedObj = ms.ToArray();
            }
            catch (Exception ex)
            {
                ErrorHandling.HandleError(ex);
            }

            return serializedObj;
        }

From GetTypeConfigurations() I get back a response like

AAEAAAD/////AQAAAAAAAAAMAgAAAE5TeXN0ZW0uRGF0YSwgVmVyc2lvbj00LjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MT............

When I try to call my ConvertFromBase64String() function, on the line

bytes = Convert.FromBase64String(content);

I get the error

The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

Even if I break on the line

dt = Functions.ConvertFromBase64String<DataTable>(response);

and I inspect the value of response without executing my Decode functions, I still get the same message even in Visual Studio:

enter image description here

And yet, in https://www.base64decode.org/ I can decode my response and get this:

enter image description here

Why can I not decode the response in Visual Studio, but the website above can? What's wrong with my "response" variable?

Upvotes: 0

Views: 96

Answers (0)

Related Questions