Boost ASIO HTTP client POST
Boost ASIO HTTP client POST
我正在尝试让 boost ASIO 库发送帖子,但变量永远不会到达服务器。 我知道服务器工作正常(使用 curl 测试)
这段代码不起作用(变量 msg 没有发布到服务器),但当我使用 curl 时它确实有效
tcp::resolver resolver(io_service);
tcp::resolver::query query("localhost", "3000"); // "http");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::resolver::iterator end;
tcp::socket socket(io_service);
boost::system::error_code error = boost::asio::error::host_not_found;
while (error && endpoint_iterator != end)
{
socket.close();
socket.connect(*endpoint_iterator++, error);
}
if (error)
throw boost::system::system_error(error);
boost::asio::streambuf request;
std::ostream request_stream(&request);
string sToSend = "msg=testing";
request_stream << "POST / HTTP/1.1rn";
request_stream << "Host: " << "localhost:3000" << "rn";
request_stream << "Accept: */*rn";
request_stream << "Content-Length: " << sToSend.length() << "rn";
request_stream << "Content-Type: application/x-www-form-urlencoded";
request_stream << "Connection: closernrn"; d
request_stream << sToSend;
// Send the request.
boost::asio::write(socket, request);
boost::asio::streambuf response;
boost::asio::read_until(socket, response, "rn");
测试我使用的服务器
$ curl --verbose -X POST -d msg=testing123 localhost:3000/
* Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> POST / HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:3000
> Accept: */*
> Content-Length: 14
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 14 out of 14 bytes
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Date: Mon, 21 Mar 2016 20:55:00 GMT
< Connection: keep-alive
< Transfer-Encoding: chunked
<
* Connection #0 to host localhost left intact
此行缺少回车符和换行符:
request_stream << "Content-Type: application/x-www-form-urlencoded";
相关文章:
- 如何使用curlpp通过POST方法上传文件和json数据
- 使用libcurl提交批量url的正确BING Api POST url是什么
- 命名空间 pqxx 无法识别 - 将 libpqxx 添加到 Windows 上的 Visual Studio Post
- FindPOCO CMAKE 脚本无法找到 MYSQL::client
- 如何使用boost/beast从HTTP POST请求中解析和提取有效负载?
- 如何使用 curl 将 POST 请求从 python 重写为 C++
- 从简单的 POST 请求中解析异常
- Visual Studio 2017 中的 cpprestapi 问题找不到 POST/GET 常量
- 使用 cpp-httplib 的 POST 请求
- boost beast Websocket Multi Request Server/Client 并不是真正的 mul
- POST Arduino 传感器值通过 Flask API 发送到本地 Sqlite db
- C++:如何通过 curl 调用使用 HTTP post 请求发送二进制数据(protobuf 数据)
- C++中的 cURL POST 请求
- C++ Schannel POST 400 错误代码错误请求
- c++curl返回413请求实体过大,但是post大小远小于max大小
- HTTP POST 请求不允许我定义上下文类型
- 如何在c++中使用libcurl发送POST请求并接收它
- cpp libcurl 在 HTTP post 调用中发送 zip 文件,而不使用 multipart\form-da
- 使用 http post 在 FCM 中推送通知
- Boost ASIO HTTP client POST