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?

3
  • 3
    I suggest you use base64 encode the data with a URL-safe base64 decodabet. Commented Dec 15, 2015 at 11:57
  • 1
    Well I'd start off by doing some research. Just searching on SO found stackoverflow.com/questions/26353710/… Commented Dec 15, 2015 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? Commented Dec 15, 2015 at 12:08

1 Answer 1

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#?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.