.NET handlepostrequest - 检索数据

发布于 2024-12-03 02:48:00 字数 1822 浏览 1 评论 0原文

我正在将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南汐寒笙箫 2024-12-10 02:48:00

干得好。

string data;
using (var streamReader = new StreamReader(Request.InputStream))
{
   data = streamReader.ReadToEnd();
}

不过,如果您只需要注册

var registration = Request["registration"];

一切基本上都在 Request 实例上,可以从 PageWebControlHttpContext.Current.Request 访问。对于 HttpHandlerHttpContext 实例会为您传入。

public void ProcessRequest(HttpContext context)
{
    ...
}

Here you go.

string data;
using (var streamReader = new StreamReader(Request.InputStream))
{
   data = streamReader.ReadToEnd();
}

Although if all you need is registration.

var registration = Request["registration"];

Everything is basically on Request instance which can be accessed from a Page, WebControl, or HttpContext.Current.Request. In the case of a HttpHandler the HttpContext instance is passed in for you.

public void ProcessRequest(HttpContext context)
{
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文