.NET 套接字网络服务器 HTTP POST 请求正文
我正在用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
maxRequestSize
是 487,那么您将得到您描述的结果。另请记住
Content-Length
不是字节 - 它是八位字节:HTTP 标头中的“Content-Length”字段是什么?(好吧,我很迂腐 - 八位字节是 8 位;))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 ;))我想知道
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?