如题,但我要获取的是整个请求数据,而不是参数,我找到一个方法,可是跟踪了一下,却发现长度为0,不知道为什么?
long l = HttpContext.Current.Request.InputStream.Length;

解决方案 »

  1.   

    我的代码如下
    protected void Page_Load(object sender, EventArgs e)
        {
            long l = HttpContext.Current.Request.InputStream.Length;    }    private string GetInput()
        {        try
            {            System.IO.Stream s = HttpContext.Current.Request.InputStream;
                int i = HttpContext.Current.Request.TotalBytes;            int count = 0;            byte[] buffer = new byte[1024];            StringBuilder builder = new StringBuilder();            while ((count = s.Read(buffer, 0, 1024)) > 0)
                {                builder.Append(Encoding.UTF8.GetString(buffer, 0, count));            }
                String s1 = builder.ToString();
                return builder.ToString();        }        catch (Exception ex)
            {            throw ex;        }    }
      

  2.   

    对不起,应该是这样:    protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write(GetInput());
                }    private string GetInput()
        {        try
            {            System.IO.Stream s = HttpContext.Current.Request.InputStream;
                int i = HttpContext.Current.Request.TotalBytes;            int count = 0;            byte[] buffer = new byte[1024];            StringBuilder builder = new StringBuilder();            while ((count = s.Read(buffer, 0, 1024)) > 0)
                {                builder.Append(Encoding.UTF8.GetString(buffer, 0, count));            }
                return builder.ToString();        }        catch (Exception ex)
            {            throw ex;        }    }
      

  3.   

    //试试这样读取
    byte[] b=new byte[Request.ContentLength];
    b=Request.BinaryRead(Request.ContentLength);
      

  4.   

    如果你是get肯定是0,你放个按纽,点一下就有数据了.
      

  5.   

    很奇怪,我这样得到的结果为i=0:
    protected void Page_Load(object sender, EventArgs e)
        {        int i = Request.ContentLength;    }
    为什么?我是直接访问的Default.aspx这个文件,应该是对的啊?
      

  6.   

    我是直接在vs2005下做的调试,得到的结果.您说的放按钮点是不是要POST的意思?
      

  7.   

    是的,直接运行,是get,加个服务器控件交互才是post
      

  8.   

    好,我试下,顺便问问,如果要获得get的数据请求流,该用什么方法?
      

  9.   

    取不到流,只能用QueryString 或是
    Request.ServerVariables
      

  10.   

    我加了个控件,现在可以得到POST的数据了,但是是乱码,程序如下:
       protected void Page_Load(object sender, EventArgs e)
        {
            int i = Request.ContentLength;
            String Content = GetInput();
            Response.Write(Content);
            CreateLog(Content);
        }    private void CreateLog(String content)
        {
            string strFilePath = Server.MapPath("log.txt");
            System.IO.FileStream fs = new System.IO.FileStream(strFilePath, System.IO.FileMode.Append);
            System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.Default);
            sw.WriteLine(content);
            sw.Close();
            fs.Close();
        }
        private string GetInput()
        {        try
            {            System.IO.Stream s = HttpContext.Current.Request.InputStream;
                int i = HttpContext.Current.Request.TotalBytes;            int count = 0;            byte[] buffer = new byte[1024];            StringBuilder builder = new StringBuilder();            while ((count = s.Read(buffer, 0, 1024)) > 0)
                {                builder.Append(Encoding.UTF8.GetString(buffer, 0, count));            }
                String s1 = builder.ToString();
                return builder.ToString();        }        catch (Exception ex)
            {            throw ex;        }    }
      

  11.   

    得到结果如下:
    __VIEWSTATE=%2FwEPDwUKMTQ2OTkzNDMyMWRkckA1z2Scp7R7xgti1sU6rWeCTZo%3D&Button1=Button&__EVENTVALIDATION=%2FwEWAgKOu6TfBgKM54rGBhb2Pbzfw1kpuox2rykmR7paJCiV 
      

  12.   

    一般这样
    Request["post或者QueryString的名称"]
    这样返回的十一个string类型的值。
      

  13.   

    整个请求数据应该包括了,请求参数,值,http头等信息,我需要的是这些内容 
      

  14.   

    是浏览器处理过后要发送的content
      

  15.   

    不是,你看我程序GetInput() ,这段,应该是获取请求的数据
      

  16.   

    补充一下,我只能说我取不到,不代表其他人。Request.ServerVariables  可以取到所有头信息。
      

  17.   

    我不大了解这两种协议,可能我觉得post使得数据被进行封装,使得安全性更高,而不是想象中的原提交数据.
    比如你上面的是viewstate机制处理的,发送到服务器端