Sending XML data via HTTP POST to IHttpHandler causes HttpRequestValidationException

前提是你 提交于 2019-12-04 04:51:44

问题


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! :)


回答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!! :)




回答2:


Just modify the web.config like this:

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


来源:https://stackoverflow.com/questions/4745872/sending-xml-data-via-http-post-to-ihttphandler-causes-httprequestvalidationexcep

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!