如何获得POST请求的数据? 如题,但我要获取的是整个请求数据,而不是参数,我找到一个方法,可是跟踪了一下,却发现长度为0,不知道为什么?long l = HttpContext.Current.Request.InputStream.Length; 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我的代码如下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; } } 对不起,应该是这样: 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; } } //试试这样读取byte[] b=new byte[Request.ContentLength];b=Request.BinaryRead(Request.ContentLength); 如果你是get肯定是0,你放个按纽,点一下就有数据了. 很奇怪,我这样得到的结果为i=0:protected void Page_Load(object sender, EventArgs e) { int i = Request.ContentLength; }为什么?我是直接访问的Default.aspx这个文件,应该是对的啊? 我是直接在vs2005下做的调试,得到的结果.您说的放按钮点是不是要POST的意思? 是的,直接运行,是get,加个服务器控件交互才是post 好,我试下,顺便问问,如果要获得get的数据请求流,该用什么方法? 取不到流,只能用QueryString 或是Request.ServerVariables 我加了个控件,现在可以得到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; } } 得到结果如下:__VIEWSTATE=%2FwEPDwUKMTQ2OTkzNDMyMWRkckA1z2Scp7R7xgti1sU6rWeCTZo%3D&Button1=Button&__EVENTVALIDATION=%2FwEWAgKOu6TfBgKM54rGBhb2Pbzfw1kpuox2rykmR7paJCiV 一般这样Request["post或者QueryString的名称"]这样返回的十一个string类型的值。 整个请求数据应该包括了,请求参数,值,http头等信息,我需要的是这些内容 是浏览器处理过后要发送的content 不是,你看我程序GetInput() ,这段,应该是获取请求的数据 补充一下,我只能说我取不到,不代表其他人。Request.ServerVariables 可以取到所有头信息。 我不大了解这两种协议,可能我觉得post使得数据被进行封装,使得安全性更高,而不是想象中的原提交数据.比如你上面的是viewstate机制处理的,发送到服务器端 查询 这样写哪里有错,各位帮帮忙,弄几个小时了,就是不行 实体类用作参数时的问题 母版内容页如何调用CSS样式 有懂FLEX的吗? 菜鸟提问:如何插入相应的数据啊? 这个问题待高手解决,在线等... 如何读取数据库中的时间 用DropDownList绑定数据,如何能显示出自定义的部分? *** 在网页文本框中输入全角,半角字符的问题 *** 请问这是什么意思 急急急。。!高分求助!!!一个网站的明星按字母顺序排列,在线等。。。。。。
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; } }
{
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; } }
byte[] b=new byte[Request.ContentLength];
b=Request.BinaryRead(Request.ContentLength);
protected void Page_Load(object sender, EventArgs e)
{ int i = Request.ContentLength; }
为什么?我是直接访问的Default.aspx这个文件,应该是对的啊?
Request.ServerVariables
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; } }
__VIEWSTATE=%2FwEPDwUKMTQ2OTkzNDMyMWRkckA1z2Scp7R7xgti1sU6rWeCTZo%3D&Button1=Button&__EVENTVALIDATION=%2FwEWAgKOu6TfBgKM54rGBhb2Pbzfw1kpuox2rykmR7paJCiV
Request["post或者QueryString的名称"]
这样返回的十一个string类型的值。
比如你上面的是viewstate机制处理的,发送到服务器端