.NET 套接字网络服务器 HTTP POST 请求正文

发布于 2024-11-29 00:34:20 字数 1385 浏览 0 评论 0原文

我正在用 C# 做一个嵌入式项目,并且编写了一个基于套接字的 Web 服务器。一切正常,除了我一生都无法获得请求正文。 Content-Length 表示有 12 个字符,但 socket.Recieve 方法仅获取标头。

while (true)
{

    using (Socket clientSocket = listeningSocket.Accept())
    {
        IPEndPoint clientIP = clientSocket.RemoteEndPoint as IPEndPoint;
        Debug.Print("Received request from " + clientIP.ToString());
        var x = clientSocket.RemoteEndPoint;

        int availableBytes = clientSocket.Available;

        Debug.Print(DateTime.Now.ToString() + " " + availableBytes.ToString() + " request bytes available");

        int bytesReceived = (availableBytes > maxRequestSize ? maxRequestSize : availableBytes);
        if (bytesReceived > 0)
        {
            byte[] buffer = new byte[bytesReceived]; // Buffer probably should be larger than this.
            int readByteCount = clientSocket.Receive(buffer, bytesReceived, SocketFlags.None);

            using (Request r = new Request(clientSocket, Encoding.UTF8.GetChars(buffer)))
            {
                Debug.Print(DateTime.Now.ToString() + " " + r.URL);
                if (requestReceived != null) requestReceived(r);

            }


        }
    }
    Thread.Sleep(10);
}

availableBytes = 499

bytesReceived = 499

readByteCount = 487(短12个字符)

我在这里缺少什么?如果有什么区别的话,正文是多部分表单数据。

I am doing an embedded project in C# and I have written a socket based web server. Everything works well, except I can't for the life of me get the request body. Content-Length says that there is 12 characters, but the socket.Recieve method only gets the headers.

while (true)
{

    using (Socket clientSocket = listeningSocket.Accept())
    {
        IPEndPoint clientIP = clientSocket.RemoteEndPoint as IPEndPoint;
        Debug.Print("Received request from " + clientIP.ToString());
        var x = clientSocket.RemoteEndPoint;

        int availableBytes = clientSocket.Available;

        Debug.Print(DateTime.Now.ToString() + " " + availableBytes.ToString() + " request bytes available");

        int bytesReceived = (availableBytes > maxRequestSize ? maxRequestSize : availableBytes);
        if (bytesReceived > 0)
        {
            byte[] buffer = new byte[bytesReceived]; // Buffer probably should be larger than this.
            int readByteCount = clientSocket.Receive(buffer, bytesReceived, SocketFlags.None);

            using (Request r = new Request(clientSocket, Encoding.UTF8.GetChars(buffer)))
            {
                Debug.Print(DateTime.Now.ToString() + " " + r.URL);
                if (requestReceived != null) requestReceived(r);

            }


        }
    }
    Thread.Sleep(10);
}

availableBytes = 499

bytesReceived = 499

readByteCount = 487 (12 characters short)

What am I missing here? The body is multipart form data if that makes any difference.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

荒岛晴空 2024-12-06 00:34:20
int bytesReceived = (availableBytes > maxRequestSize ? maxRequestSize : availableBytes);

如果 maxRequestSize 是 487,那么您将得到您描述的结果。

另请记住 Content-Length 不是字节 - 它是八位字节:HTTP 标头中的“Content-Length”字段是什么?(好吧,我很迂腐 - 八位字节是 8 位;))

int bytesReceived = (availableBytes > maxRequestSize ? maxRequestSize : availableBytes);

If maxRequestSize is 487, then you will get the results you describe.

Also remember Content-Length is not bytes - it's octets: What's the "Content-Length" field in HTTP header? (OK I'm being pedantic - and octet is 8 bits ;))

失去的东西太少 2024-12-06 00:34:20

我想知道 Available 属性有多可靠。看起来它只对非阻塞套接字有用,然后仅作为布尔标志表示某物可用。

为什么不直接循环读取流并解析消息呢?

I wonder how reliable that Available property is. It looks like it's only useful for non-blocking sockets, and then only as a boolean flag saying that something is available.

Why not just read the stream in a loop parsing messages as you go?

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