I have a REST WCF service that has a method that gets a parameter as a string. This string can contain slash / character. It makes my request wrong, as I think the URL goes wrong.
When requesting it and getting response (WebRequest.GetResponse()) throws "The remote server returns an error: (400) Bad Request." exception.
My request: http://localhost:17679/testmethod/DmC/TCGlOLz1EbEwqAls5Q==\nh2cQzTizSBg=
I tried to use Uri.EscapeDataString, but it does not help, I get the same exception as above. After this conversion my request looks like this: http://localhost:17679/testmethod/DmC%2FTCGlOLz1EbEwqAls5Q%3D%3D%0Ah2cQzTizSBg%3D
If I pass a string without slash in the string it works as I want.
How can I pass slash and other 'url sensitive' characters to a WCF REST service?
Thx.
UPDATE: I solved it, you can see it in my answer bellow.
Try forcing a \ before each /. (normally it forces the metacharacters to be read as a normal character)
I solved it.
URI template is the key.
If I define URI this way, it produces the exception above:
By modifying this way, it works:
Anyway, Uri.EscapeDataString is needed!
While the accepted answer will work in some cases, where the URI parameter is at the end of the URI, it will not work if a URI parameter is in the middle of a URI. But with just a few configuration settings, you can allow your application to accept encoded forward slashes.
The
HttpListener
that takes incoming requests uses an internalHttpListenerRequestUriBuilder
to parse the raw request URI.The
HttpListenerRequestUriBuilder
will or will not unescape the encoding based on a setting. Add the following setting to yourapp.config
file:This will allow the incoming
Message
'sTo
headers to be correctly built without unescaping the URIs.If you are using a version of .NET before 4.5, I believe you may also need to add another setting instructing the
System.Uri
class to not escape slashes forhttp
andhttps
paths. This setting is as follows:Uri.Match
and the defaultQueryStringConverter
should still work with the unescaped text, so a method like:will provide an unescaped string to the
bar
parameter.