Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass slash and other 'url sensitive' characters to a WCF REST service?

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.

like image 286
Tom Avatar asked Aug 24 '11 14:08

Tom


2 Answers

I solved it.

URI template is the key.

If I define URI this way, it produces the exception above:

[OperationContract()]
[WebGet(UriTemplate = "/testmethod/{testvalue}"/*, BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml*/)]
string TestMethod(string testvalue);

By modifying this way, it works:

[OperationContract()]
[WebGet(UriTemplate = "/testmethod?v={testvalue}"/*, BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml*/)]
string TestMethod(string testvalue);

Anyway, Uri.EscapeDataString is needed!

like image 64
Tom Avatar answered Sep 30 '22 08:09

Tom


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 internal HttpListenerRequestUriBuilder to parse the raw request URI.

The HttpListenerRequestUriBuilder will or will not unescape the encoding based on a setting. Add the following setting to your app.config file:

<configuration>
  <system.net>
    <settings>
      <httpListener unescapeRequestUrl="false"/>
    </settings>
  </system.net>
</configuration>

This will allow the incoming Message's To 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 for http and https paths. This setting is as follows:

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

Uri.Match and the default QueryStringConverter should still work with the unescaped text, so a method like:

[WebGet(UriTemplate = "foos/{bar}/baz"]
public Baz GetFooBaz(string bar)
{
    // ...
}

will provide an unescaped string to the bar parameter.

like image 45
Joseph Nields Avatar answered Sep 30 '22 06:09

Joseph Nields