在C#中检索ASP.NET POST数据 [英] Retrieving POST data in C# ASP.NET
本文介绍了在C#中检索ASP.NET POST数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有麻烦,使我的程序正常工作 - 在这里我解释一下:
- 我有,一方面是C#的WinForms应用程序,它通过使用导航的方式启动IE浏览器的一个实例:
myWebBrowser.Navigate(myUrl,_blank,intarray,);
与intarray
是这样定义的:字节[] = intarray BitConverter.GetBytes(ID);
。在这方面,它的工作原理。 -
在另一边,我也有来检索
intarray
的ASP .NET应用程序的WebForms。我已经试过这一点。如果(HttpContext.Current!= NULL)
{
如果(会话[认证] == NULL)
{
VAR电流= HttpContext.Current;
字节[] = POSTDATA getpostdata(电流);
}
}私人字节[] getpostdata(HttpContext的CurrentContext)
{
MemoryStream的毫秒=新的MemoryStream();
CurrentContext.Request.InputStream.CopyTo(毫秒);
字节[] = POSTDATA ms.ToArray();
返回POSTDATA;
}
//将一个字节数组对象
公众诠释ByteArrayToInt(字节[] arrBytes)
{
如果(BitConverter.IsLittleEndian)Array.Reverse(arrBytes);
INT I = BitConverter.ToInt32(arrBytes,0);
返回我;
}
这个问题似乎是在 getpostdata(HttpContext的)
函数检索数据...我得到长度= 0,而不是一个这是一个字节数组长度= 4发...
有谁知道如何使它工作?
晏
解决方案
无功电流= HttpContext.Current;
VAR SR =新的StreamReader(Request.InputStream,Encoding.Default);
变种POSTDATA = sr.ReadToEnd();
以上
I'm having troubles to make my program work correctly - here I explain :
- I have, on one hand a C# WinForms app which launches an instance of IE by using the "Navigate" method :
myWebBrowser.Navigate(myUrl, "_blank", intarray, "");
, withintarray
defined like this :byte[] intarray = BitConverter.GetBytes(id);
. On this side, it works. On the other side, I have an ASP .NET WebForms application which has to retrieve this
intarray
. I've tried this.if (HttpContext.Current != null) { if (Session["Authenticated"] == null) { var current = HttpContext.Current; byte[] postdata = getpostdata(current); } } private byte[] getpostdata(HttpContext CurrentContext) { MemoryStream ms = new MemoryStream(); CurrentContext.Request.InputStream.CopyTo(ms); byte[] postdata = ms.ToArray(); return postdata; } // Convert a byte array to an Object public int ByteArrayToInt(byte[] arrBytes) { if (BitConverter.IsLittleEndian) Array.Reverse(arrBytes); int i = BitConverter.ToInt32(arrBytes, 0); return i; }
The problem seems to be in retrieving the Data in the getpostdata(HttpContext)
function... I get a byte array with length = 0 instead of the one which is sent with length = 4...
Does anyone know how to make it work ?
Yann
解决方案
var current = HttpContext.Current;
var sr = new StreamReader(Request.InputStream, Encoding.Default);
var postdata = sr.ReadToEnd();
above
这篇关于在C#中检索ASP.NET POST数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文