GETting a URL with an url-encoded slash

.net, base64, c#, http, url-encoding

Solution

By default, the `Uri` class will not allow an escaped `/` character (`%2f`) in a URI (even though this appears to be legal in my reading of RFC 3986).

Uri uri = new Uri("http://example.com/%2F");
Console.WriteLine(uri.AbsoluteUri); // prints: http://example.com//

(Note: don't use Uri.ToString to print URIs.)

According to the bug report for this issue on Microsoft Connect, this behaviour is by design, but you can work around it by adding the following to your app.config or web.config file:

<uri>
  <schemeSettings>
    <add name="http" genericUriParserOptions="DontUnescapePathDotsAndSlashes" />
  </schemeSettings>
</uri>

(Reposted from https://stackoverflow.com/a/10415482 because this is the "official" way to avoid this bug without using reflection to modify private fields.)

Edit: The Connect bug report is no longer visible, but the documentation for `<schemeSettings>` recommends this approach to allow escaped `/` characters in URIs. Note (as per that article) that there may be security implications for components that don't handle escaped slashes correctly.

Problem

I want to send a HTTP GET to `http://example.com/%2F`. My first guess would be something like this: ``` using (WebClient webClient = new WebClient()) { webClient.DownloadData("http://example.com/%2F"); } ``` Unfortunately, I can see that what is actually sent on the wire is: ``` GET // HTTP/1.1 Host: example.com Connection: Keep-Alive ``` So http://example.com/%2F gets translated into http://example.com// before transmitting it. Is there a way to actually send this GET-request? The OCSP-protocol mandates sending the url-encoding of a base-64-encoding when using OCSP over HTTP/GET, so it is necessary to send an actual %2F rather than an '/' to be compliant. EDIT: Here is the relevant part of the OCSP protocol standard (RFC 2560 Appendix A.1.1): An OCSP request using the GET method is constructed as follows: GET {url}/{url-encoding of base-64 encoding of the DER encoding of the OCSPRequest} I am very open to other readings of this, but I cannot see what else could be meant.

Original source

Related problems