1

I need to pass a byte array via a URL. So I am encoding it with the UrlEncode Method like this:

string ergebnis = HttpUtility.UrlEncode(array);

The result is the following string: %00%00%00%00%00%25%b8j

Now when I pass this string in a URL like this http://localhost:51980/api/Insects?Version=%00%00%00%00%00%25%b8j

This is my Get function:

        public List<TaxonDiagnosis> Get([FromUri] string version)
        {
            List<TaxonDiagnosis> result = new List<TaxonDiagnosis>();

            result = db.TaxonDiagnosis.ToList();

            byte[] array = HttpUtility.UrlDecodeToBytes(version);

            if (version != null)
                result = db.GetTaxonDiagnosis(array).ToList();

            return result;
        }

The problem is, version's value isn't %00%00%00%00%00%25%b8j. Instead it is this \0\0\0\0\0%�j. This of course causes problems when I try to decode it into a byte array again.

How can I pass the correct string in the Url?

adryr
  • 140
  • 2
  • 14
  • 3
    I suggest you use base64 encode the data with a URL-safe base64 decodabet. – Jon Skeet Dec 15 '15 at 11:57
  • 1
    Well I'd start off by doing some research. Just searching on SO found https://stackoverflow.com/questions/26353710/how-to-achieve-base64-url-safe-encoding-in-c – Jon Skeet Dec 15 '15 at 12:06
  • It seems that `version` is already url-decoded. But it's kinda complicated to get bytes out of a string, due to encodings (as you can see %25%b8 ends up as a unicode character in your string). I don't have detailed knowledge about those web services...isn't it possible to change the method's argument from `string version` to `byte[] array`? – René Vogt Dec 15 '15 at 12:08

1 Answers1

2

As suggested by Jon Skeet, I encoded the arraywith a URL-safe base64 decodabet like in this post: How to achieve Base64 URL safe encoding in C#?

adryr
  • 140
  • 2
  • 14