gpt4 book ai didi

c++ - boost asio read_until 文件异常结尾?

转载 作者:搜寻专家 更新时间:2023-10-31 02:23:22 28 4
gpt4 key购买 nike

我正在尝试通过 boost asio 向服务器发送 HTTP 请求。我有以下用 C++ 创建的请求:

static const std::string TEMPLATE_HEADER =
"POST " + HTTP_PATH + " HTTP/1.1\r\n"
"Content-Type: application/json\r\n"
"Host: " + HOST_TAG + "\r\n"
"{ " + TEMPLATE_BODY + " }\r\n\r\n";

我发送的实际请求是这样的:

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(Google 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 + "\r\n\r\n"
  2. 您可能需要发送 Content-Length header ( What's the "Content-Length" field in HTTP header? )。 postman 可能会为您添加

关于c++ - boost asio read_until 文件异常结尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29384314/

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