gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:14:08 27 4
gpt4 key购买 nike

我正在用 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);
}

可用字节数 = 499

bytesReceived = 499

readByteCount = 487(短 12 个字符)

我在这里错过了什么?主体是多部分表单数据(如果有任何区别的话)。

最佳答案

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

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

还要记住 Content-Length 不是字节 - 它是八位字节:What's the "Content-Length" field in HTTP header? (好吧,我太迂腐了——八位字节是 8 位;))

关于c# - .NET 套接字网络服务器 HTTP POST 请求正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7011517/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com