Boost ASIO HTTP client POST(增强ASIO HTTP客户端开机自检)
本文介绍了增强ASIO HTTP客户端开机自检的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试让Boost ASIO库发送POST,但变量从未到达服务器。我知道服务器工作正常(使用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.1
";
request_stream << "Host: " << "localhost:3000" << "
";
request_stream << "Accept: */*
";
request_stream << "Content-Length: " << sToSend.length() << "
";
request_stream << "Content-Type: application/x-www-form-urlencoded";
request_stream << "Connection: close
"; d
request_stream << sToSend;
// Send the request.
boost::asio::write(socket, request);
boost::asio::streambuf response;
boost::asio::read_until(socket, response, "
");
要测试我使用的服务器
$ 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";
这篇关于增强ASIO HTTP客户端开机自检的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
织梦狗教程
本文标题为:增强ASIO HTTP客户端开机自检
基础教程推荐
猜你喜欢
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 初始化列表*参数*评估顺序 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- CString 到 char* 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
