提升asio read_u直到文件的异常结束

Boost asio read_until exception end of file?

本文关键字:文件 异常 结束 asio read 提升      更新时间:2023-10-16

我正试图通过boost asio向服务器发送HTTP请求。我在c++中创建了以下请求:

static const std::string TEMPLATE_HEADER =
"POST " + HTTP_PATH + " HTTP/1.1rn"
"Content-Type: application/jsonrn"
"Host: " + HOST_TAG + "rn"
"{ " + TEMPLATE_BODY + " }rnrn";

我发送的实际请求是:

POST /eikon/authsession/machines/ HTTP/1.1 Content-Type: application/json Host: amers1.am.cp.icp2.mpp.ime.reuters.com:80 { "machineID" : "EP-03A482F1B88A","password" : "PasswordReset","takeESOControl" : "false","deviceID" : "123" }

当使用Postman(一个谷歌Chrome扩展)时,这是有效的,但使用boost asio似乎不起作用。我做以下操作(上面的HTTP请求是strTemp.

// Write the request to the buffer
size_t request_length = strTemp.length();
boost::asio::write(s, boost::asio::buffer(strTemp, request_length));
char reply[max_length];
// When a reply is received ending in the correct tag we are looking for, store it in the response buffer
boost::asio::streambuf response;
boost::asio::read_until(s, response, Constants::BRACE_TAG);

其中BRACE_TAG变量="}"(返回的响应以大括号结尾。

我收到一个异常,上面写着"exception:read_until:End of file"。

有人知道发生了什么事吗?

谢谢。

  1. 在标题和正文之间应该有一条双换行符:

    "Host: " + HOST_TAG + "rnrn"
    
  2. 您可能需要发送Content-Length标头(HTTP标头中的"Content-Length"字段是什么?)。邮差可能会为你添加