简体   繁体   English

不使用套接字接收HTTP正文

[英]Not recieving HTTP body with Sockets

I'm trying to build a super simple JSON-RPC server and I'm having problems receiving the body of a POST message. 我正在尝试构建一个超级简单的JSON-RPC服务器,并且在接收POST消息正文时遇到问题。 I can receive GET messages fine, since the arguments are just included in the Host Header. 我可以很好地接收GET消息,因为参数仅包含在主机头中。 But I can't receive the body of either a POST or a PUT. 但是我无法接收POST或PUT的正文。 I've used two different versions of Postman and cUrl to verify that I actually am sending a proper POST message. 我使用了两个不同版本的Postman和cUrl来验证我实际上是在发送正确的POST消息。

My code is fairly simple; 我的代码很简单; I create a socket, bind to it, then listen and save what's been received. 我创建一个套接字,将其绑定,然后监听并保存收到的内容。 (P is the reference to the Program class) (P是对Program类的引用)

p.sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
p.sock.Bind(new IPEndPoint(IPAddress.Any, p.config.server_port));
p.sock.Listen(5);
p.serverThread = new Thread(new ThreadStart(() => serverListen(p)));
p.serverThread.Start();

and then I use the following block to actually receive messages: 然后使用以下代码块实际接收消息:

Socket recieved = p.sock.Accept();
//handle recieved info
string message = "";
byte[] buffer = new byte[10000];
int readResult = recieved.Receive(buffer);
if (readResult > 0) {
    message = Encoding.UTF8.GetString(buffer,0, readResult);
    message = message;//for debugging purposes
    message = ReceivedMessage.decodeURL(message);//Get rid of URL encoding
}

This works great for GET messages, but fails to return the body for POST messages. 这对于GET消息很有用,但无法返回POST消息的正文。 The above code retrieves the following HTTP message: 上面的代码检索以下HTTP消息:

POST / HTTP/1.1
cache-control: no-cache
Postman-Token: 5dab0e8e-910f-4c5d-a7dd-49bf5348a8d0
Content-Type: text/plain
User-Agent: PostmanRuntime/3.0.5
Accept: */* 
Host: localhost:1215
accept-encoding: gzip, deflate
content-length: 18
Connection: keep-alive

Which doesn't contain any body elements at all. 根本不包含任何身体元素。 What am I missing? 我想念什么? Any help would be greatly appreciated. 任何帮助将不胜感激。

So the error was that the HTTP headers are sent first, then the body is sent afterwards. 因此,错误是先发送HTTP标头,然后再发送正文。 Or at least I think that's what is happening. 或者至少我认为这就是正在发生的事情。 So in order to read the body, you have to make another call to Socket.Recieve() (or the Async version) to get the body text. 因此,为了读取正文,您必须再次调用Socket.Recieve()(或Async版本)以获取正文。 Which can look like this: 看起来像这样:

//get the rest of the body
string httpBody = string.Empty;
byte[] bodyBuffer = new byte[10000];    //arbitrary buffer length
string[] lines = message.Split('\n');   //split the header Message by newlines
string StrLength = string.Empty;
int contentLength = 0;
foreach (string line in lines) {
    if (line.Contains("content-length")) {
        string[] splitLine = line.Split(':');
        StrLength = splitLine[1];
        break;
    }
}
if (StrLength != null) {
    try {
        contentLength = int.Parse(StrLength);
    }
    catch {
        Console.WriteLine("Failed to parse content-length");
    }
}
else {
    Console.WriteLine("content-length header not found");
}
int bodyReadResult = sock.Receive(bodyBuffer);
if (bodyReadResult != contentLength) {
    Console.WriteLine("Failed to read all of HTTP body, probably sent as a multipart");
}
httpBody = Encoding.ASCII.GetString(bodyBuffer, 0, bodyReadResult);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM