4

I'm trying to send a request to a web service that requires certain . characters in the request URI to be percent-encoded as %2e. I'm having trouble sending a request from .NET, because when I construct the Uri instance it replaces the %2e characters with . characters. How can I work around this issue to send the web request in the form required by the service?

Edit: Here is how I'm constructing the request:

Uri uri = ...;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
request.Accept = "application/json";
request.UserAgent = "MyUserAgent";

I have attempted the following:

  • Constructing the Uri instance using UriTemplate.BindByName
  • Constructing the Uri instance, then using UriBuilder to try to insert the %2e characters.

    Uri uri = "...";
    UriBuilder builder = new UriBuilder(uri);
    builder.Path = builder.Path.Replace(".", "%2e");
    uri = builder.Uri;
    
  • Constructing the Uri while setting the dontEscape constructor argument to true.

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
  • What method are you using to send the request? Can't you use a string instead of an `Uri` object? – Guffa Nov 15 '13 at 02:21
  • what did you try? (as in, can you show us how you try to do that) – Noctis Nov 15 '13 at 02:21
  • @Guffa `WebRequest.Create` has an overload that takes a `string`, but all it does is create a `Uri` object from the string and call the other constructor. – Sam Harwell Nov 15 '13 at 02:54
  • @Noctis I added more detail, but the real key here is to create a [`Uri`](http://msdn.microsoft.com/en-us/library/system.uri.aspx) instance that contains the substring `%2e` in the path component of an HTTP or HTTPS URI. – Sam Harwell Nov 15 '13 at 02:55
  • did you look at [this](http://stackoverflow.com/a/3857067/1698987) answer ? – Noctis Nov 15 '13 at 08:32
  • @Noctis Yes, and many more. Most were about URIs with a path segment with a dot at the end, that didn't need to be escaped. This is a completely different situation because the dot isn't at the end, and *must* be escaped. – Sam Harwell Nov 15 '13 at 12:04
  • I see, I'll take it you tried with a '@' before the string? (just making sure ...) – Noctis Nov 15 '13 at 12:06
  • The @ before a string changes how you escape characters in the string. Adding it has no effect on `%2e` and doesn't change how the string works once it is created. – Guffa Nov 15 '13 at 14:19
  • 1
    I just ran into this - fortunately, the service I am calling does not **require** the dot to be encoded to %2E. But I find it annoying that `Uri` is doing this automatically and there seems to be no way of disabling it. – Nate Barbettini Nov 19 '15 at 21:47

0 Answers0