我必须在 C++ 中使用纯套接字发送后请求。我不明白将请求正文发送到哪里:
int *binary = new int[bufferLength];
...
std::stringstream out(data);
out << "POST /push1_pub?id=game HTTP/1.1\n";
out << "Host: http://0.0.0.0:80\n";
out << "Content-Length: ";
out << bufferLength*sizeof(int);
out << "\r\n\r\n";
out << binary;
if (socket.send(data.c_str(), data.size()) == -1)
{
std::cout << "Failed to send headers\n";
}
else
{
// Send request body
socket.send(reinterpret_cast<char*>(binary), bufferLength*sizeof(int));
// Get the answer of server
char buf[1024];
std::cout << socket.recv(buf, 1024) << std::endl;
std::cout << buf << std::endl;
}
但是在buf
发送正文后,我有:
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>
这里有什么问题?
更新:
由于您的评论,我写了新的标题:
std::string data;
std::stringstream out(data);
out << "POST /push1_pub?id=game\r\n";
out << "Host: http://localhost\r\n";
out << "Content-Length: ";
out << bufferLength << "\r\n";
out << "\r\n\r\n";
out << binary;
仍然有同样的问题。
更新2
使用此命令:curl -s -v -X POST 'http://0.0.0.0/push1_pub?id=game' -d 'Test'
一切正常,并正确生成和发送发布请求。