2

I'm writing an IHttpHandler implementation that will receive XML data sent through a regular HTTP POST from another website. Here's a prototype of the implementation:

public class MyHandler : IHttpHandler
{
   public void ProcessRequest(HttpContext context)
   {
      string s = context.Request.Form["input"]; // <== this throws HttpRequestValidationException
      XmlDocument doc = new XmlDocument();
      doc.LoadXml(s);
      // ...
   }

   public bool IsReusable
   {
      get { return false; }
   }
}

I'm testing the implementation with this simple page:

<body>
   <form method="post" action="MPSConnector.Results.dsvc">
      <textarea name="input"></textarea>
      <input type="submit" value="Go!" />
   </form>
</body>

The problem is that when i try to read the "input" value from the posted data, if it contains an xml string, all i get is a HttpRequestValidationException. I tried using

<pages validateRequest="false">

in web.config, and putting the validate="false" attribute in handler declaration in the httpHandles section, without results.

How can I read posted xml in my handler? (please note that i HAVE to use an IHttpHandler for this task).

EDIT: Framework version: 4.0, IIS 7.x

Thank you all! :)

Incognito
  • 16,567
  • 9
  • 52
  • 74
Claudio Valerio
  • 2,302
  • 14
  • 24

2 Answers2

0

Just modify the web.config like this:

   <location path="Handlers/MyHandler.ashx">
      <system.web>
         <httpRuntime requestValidationMode="2.0" />
      </system.web>
   </location>
Edgar
  • 473
  • 1
  • 5
  • 19
0

As far as I know, you just need to encode that XML with entities.

I mean that < should be & lt; or > & gt;, and so on.

EDIT: I found that this is a duplicate of: How can Request Validation be disabled for HttpHandlers?

Try this!! :)

Community
  • 1
  • 1
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
  • Hi Matias, and thank for your answer. Please note that i have no control on data that the handler will receive, the page i have posted is just an example for testing. I have to presume that xml data will come with no encoding, basing on the documentation of the system that will send the data in production environment. – Claudio Valerio Jan 20 '11 at 10:50
  • Have you tried with an HTTP Module? Try if you receive the data and you can encode there if it's a raw XML, and then your handler will receive the encoded data. – Matías Fidemraizer Jan 20 '11 at 10:55
  • The problem is the request does not contain only raw XML (in this case all i need to do is to read xml from InputStream of Request object), but an entire form posted with one of its values containing an XML string. When i try to read this value ("input" in my semplified test, but the request will contain other non-xml values) i get the exception. I tried using an IHttpModule instead of an IHttpHandler, but when i try to access to the Request.Form NameValueCollection the exception occurs. By the way, i'd prefer not to use too many layers over the request management, because the application... – Claudio Valerio Jan 20 '11 at 11:33
  • ...puts its own logic over that (with an abstraction layer that dynamically creates the right handler when a request is made to a specific entry point). – Claudio Valerio Jan 20 '11 at 11:39