Sending XML data via HTTP POST to IHttpHandler causes HttpRequestValidationException

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! 🙂

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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!! 🙂

Method 2

Just modify the web.config like this:

   <location path="Handlers/MyHandler.ashx">
      <system.web>
         <httpRuntime requestValidationMode="2.0" />
      </system.web>
   </location>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x