.NET handlepostrequest - 检索数据
我正在将 POST 数据发送到 .NET 简单服务器示例。在标头中,我还有一些其他详细信息,它们作为输入流打包到 POST 数据发送中。如何使用 HandlePostRequest 检索它们?我的源代码附在这里:
public void handlePOSTRequest() {
Console.WriteLine("get post data start");
int content_len = 0;
MemoryStream ms = new MemoryStream();
if (this.httpHeaders.ContainsKey("content-length")) {
content_len = Convert.ToInt32(this.httpHeaders["content-length"]);
if (content_len > MAX_POST_SIZE) {
throw new Exception(
String.Format("POST Content-Length({0}) too big for this simple server",
content_len));
}
byte[] buf = new byte[BUF_SIZE];
int to_read = content_len;
while (to_read > 0) {
Console.WriteLine("starting Read, to_read={0}",to_read);
int numread = this.inputStream.Read(buf, 0, Math.Min(BUF_SIZE, to_read));
Console.WriteLine("read finished, numread={0}", numread);
if (numread == 0) {
if (to_read == 0) {
break;
} else {
throw new Exception("client disconnected during post");
}
}
to_read -= numread;
ms.Write(buf, 0, numread);
}
ms.Seek(0, SeekOrigin.Begin);
}
else
{
Console.WriteLine("Missing content length");
}
Console.WriteLine("get post data end");
srv.handlePOSTRequest(this, new StreamReader(ms));
}
我得到的所有内容都是 content_length,但我需要从流中获取数据。通过 inputStream = new BufferedStream(socket.GetStream()); 收集流在这个流中我有一个值“registration”=“123456789”,如何检索它?
谢谢
I am sending POST data to .NET simple server example. Among headers I have some other details, which are packed as a input stream into the POST data send. How to retrieve them with HandlePostRequest? My source code is attached here:
public void handlePOSTRequest() {
Console.WriteLine("get post data start");
int content_len = 0;
MemoryStream ms = new MemoryStream();
if (this.httpHeaders.ContainsKey("content-length")) {
content_len = Convert.ToInt32(this.httpHeaders["content-length"]);
if (content_len > MAX_POST_SIZE) {
throw new Exception(
String.Format("POST Content-Length({0}) too big for this simple server",
content_len));
}
byte[] buf = new byte[BUF_SIZE];
int to_read = content_len;
while (to_read > 0) {
Console.WriteLine("starting Read, to_read={0}",to_read);
int numread = this.inputStream.Read(buf, 0, Math.Min(BUF_SIZE, to_read));
Console.WriteLine("read finished, numread={0}", numread);
if (numread == 0) {
if (to_read == 0) {
break;
} else {
throw new Exception("client disconnected during post");
}
}
to_read -= numread;
ms.Write(buf, 0, numread);
}
ms.Seek(0, SeekOrigin.Begin);
}
else
{
Console.WriteLine("Missing content length");
}
Console.WriteLine("get post data end");
srv.handlePOSTRequest(this, new StreamReader(ms));
}
Everything I get is a content_length, but I need to get data from stream. The stream is gathered by inputStream = new BufferedStream(socket.GetStream()); And in this stream I have a value "registration"="123456789", how to retrieve it?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
干得好。
不过,如果您只需要
注册
。一切基本上都在
Request
实例上,可以从Page
、WebControl
或HttpContext.Current.Request
访问。对于HttpHandler
,HttpContext
实例会为您传入。Here you go.
Although if all you need is
registration
.Everything is basically on
Request
instance which can be accessed from aPage
,WebControl
, orHttpContext.Current.Request
. In the case of aHttpHandler
theHttpContext
instance is passed in for you.