我将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”,如何检索它?
由于
答案 0 :(得分:1)
你走了。
string data;
using (var streamReader = new StreamReader(Request.InputStream))
{
data = streamReader.ReadToEnd();
}
虽然你需要的只是registration
。
var registration = Request["registration"];
所有内容基本上都在Request
个实例上,可以从Page
,WebControl
或HttpContext.Current.Request
访问。在HttpHandler
的情况下,HttpContext
实例将为您传入。
public void ProcessRequest(HttpContext context)
{
...
}