日期:2014-05-20  浏览次数:20376 次

自己做的HttpHandler无法得到POST上来的数据,请问这是为什么。
我自己做了一个类实现了IHttpHandler和IRequiresSesssionState接口:

public   class   MyHttpHandler   :   IHttpHandler,   IRequiresSessionState
{
    public   void   ProcessRequest(HttpContext   context)
    {
        usingCount++;
        context.Response.Write(context.Request[ "ParamThroughGet "]);
        context.Response.Write(context.Request[ "ParamThroughPost "]);
        context.Response.End();
    }

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

web.config中配置如下:
<httpHandlers>
  <add   verb= "* "   path= "*.agt "   type= "Trying.MyHttpHandler,   MyHttpHandler "   />
</httpHandlers>

在IIS中加入了文件类型*.agt,指定允许包括GET,POST在内的所有方法,并映射到aspnet_isapi.dll进行处理。

客户端页面作了一个静态html进行测试
<html>
  <body>
    <form   id= "Form1 "   method= "post "   action= "test.agt?ParamThroughGet=123 ">
      <input   id= "ParamThroughPost "   type= "text ">
      <input   id= "Ok "   type= "submit ">
    </form>
  </body>
</html>

结果是:
对于用GET方法提交的参数,MyHttpHandler可以得到参数值(ParamThroughGet),但对于POST方法提交的form中的数据,就得不到了,总是返回null。
请高人指点一下,这是为什么。
谢谢了

------解决方案--------------------
context.Response.Write(context.Form[ "ParamThroughPost "]);

从这个问题看,你做软件有些危险,好大喜功难以成大事。
------解决方案--------------------
if(context.Request.HttpMethod.ToLower()== "get ")
context.Response.Write(context.Request[ "ParamThroughGet "]);
else
if(context.Request.HttpMethod.ToLower()== "post ")
context.Response.Write(new StreamReader(context.request.InputStream).ReadToEnd());
------解决方案--------------------
好像表单的input应该用name标识而不是id吧