2

Using this code...

using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, 
"https://example.com/?var=a%3Ab%3Dc%26d%3Fe")
{
    // Do stuff
}

...request.RequestUri now has the value "https://example.com/?var=a:b%3Dc%26d%3Fe"

Notice the %3A has been unencoded to a colon character, but all other encoded characters remain encoded.

How can I prevent the %3A from being unencoded like all the other encoded characters?

For context, I'm calling an external API. The colon throws an error. I have no control over the external API, so need to find a way of conforming to the API's encoding expectations.

Chas
  • 111
  • 5
  • From where the string "https://example.com/?var=a%3Ab%3Dc%26d%3Fe" came from ? – Orel Eraki Aug 30 '18 at 23:36
  • As per my answer, I don't think this is a problem in ASP.NET Core, but only .NET Frameworks. Can you confirm whether you're actually using ASP.NET Core? If not, you should remove it from the title and tags. – Adrian Sanguineti Aug 31 '18 at 05:47
  • 1
    Adrian, thanks. I can confirm the target framework was .NET Core 1.1. I upgraded to .NET Core 2.0 to see if that might fix the problem. No luck. I'll look into upgrading to .NET Core 2.1.401, since you seem to have found the fix in that version. Will post results on how that goes. – Chas Aug 31 '18 at 18:51
  • @Chas, no problems. You might want to also fix the tagging of your question then to be .net-core or .net-core-1.1 instead of asp.net, .net, asp.net-core-mvc so that its clear that's the platform your question relates to. – Adrian Sanguineti Sep 03 '18 at 06:48

1 Answers1

5

I thought that the behaviour describe was a bit bizarre, so I decided to test this for myself, and indeed in .NET Framework 4.5.1 and 4.7.1, the %3A is decoded as a : by the HttpRequestMessage class.

.NET 4.5.1: dotnet 451 example

.NET 4.7.1: dotnet 471 example

However in .NET Core 2.1.401, this same behaviour does not ocurr with the original string being left intact.

.NET Core 2.1.401: dotnet core example

This is therefore most likely a bug with the HttpRequestMessage implementation in the .NET Framework which was not copied across to .NET Core (which is complete re-write).

I don't have the time to do it myself, but I would recommend raising a bug in the .NET Framework GitHub project, referencing this stack overflow post.

You can also post your issue on the .NET Developer Community to also get some suggestions and workarounds.

Apart from that, I can't think on any workaround to your problem.

UPDATE 1:

Just as I was about to move onto something else, I realized that its the System.Uri class that is actually doing this bug when doing a ToString().

Take the following code as an example:

Uri uri = new Uri("https://example.com/?var=a%3Ab%3Dc%26d%3Fe");
var original = uri.OriginalString;
var toString = uri.ToString();

When executed in .NET Framework: uri.ToString()

As you can see, the original string is correct, but the result of ToString() is wrong.

UPDATE 2:

Now that it's was more obvious what is going on, I was able to dig up this old stackoverflow post: System.Uri and encoded colon (:) and this social MSDN post. Both created quite some time ago and looks like the .NET team never decided to address. I can't get to the Microsoft Connect bug raised from where I am to find out what it resulted in, but the .NET Framework GitHub project and .NET Developer Community pages still may be the best places to get a response from the .NET team directly. (The social MSDN site is mostly non-Microsoft people so will not be as helpful).

Adrian Sanguineti
  • 2,455
  • 1
  • 27
  • 29