The obsolete Uri(string, bool)
constructor is used to construct a URI from an already escaped string (obsolete presumably not to break the program if an invalid string is presented). However, I find myself in a situation where I need to pass literal bytes via the URI, and I can't think of a better way to encode them.
I am constructing a data:
URI, which is a standardized way to pass the whole resource instead of its identifier. Although I am aware it has a ;base64
specifier to mark the passed data as encoded in base64, there are situations when the URI is shorter without base64, for example when there are less binary data. Because I don't want to worry about encodings, I simply want to pass the bytes together with the URI as an URI-encoded string, using HttpUtility.UrlEncode(byte[])
.
Since I am practically left with no other choice than to let .NET encode the string for me, without having to use obsolete constructors, and there is no Uri(byte[])
constructor (there should be, in my opinion), what are my options to construct the URI?
I thought about using Encoding.GetEncoding(1252)
to create a string from the bytes and use that, as cp1252 can decode any character, but it seems the internal Uri encoding method uses UTF-8 to encode the characters, so I don't find it possible to use a text encoding at all.
What are my options? Is it okay to continue using the obsolete constructor, if there is no other way?
Well, the standard Uri constructor accepts pre-encoded URIs, and doesn't replace valid
%
characters, so using the dontReplace parameter isn't really necessary when constructing Uri from a valid URI string containing encoded parts. They won't get re-encoded.