我一直试图让这工作几天,但我一直从服务器收到400错误。BOOST ASIO POST HTTP请求 - 标头和正文
基本上,我想要做的是发送一个HTTP POST请求到一个服务器,需要一个JSON请求正文与一些属性。
这些是我目前使用
修订--- 13年7月23日上午10:00刚刚注意到我使用而不是HTTP不知道这将如何多大效果HTTP调用,但TCP的库使用纯HTTP与BOOST我无法找到客户的任何示例:: ASIO
#include <iostream>
#include <istream>
#include <ostream>
#include <string>
#include <boost/asio.hpp>
#include <sstream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
using boost::property_tree::ptree; using boost::property_tree::read_json; using boost::property_tree::write_json;
using boost::asio::ip::tcp;
SET UP CODE
// Get a list of endpoints corresponding to the server name.
tcp::resolver resolver(io_service);
tcp::resolver::query query(part1, "http");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
// Try each endpoint until we successfully establish a connection.
tcp::socket socket(io_service);
boost::asio::connect(socket, endpoint_iterator);
// Form the request. We specify the "Connection: close" header so that the
// server will close the socket after transmitting the response. This will
// allow us to treat all data up until the EOF as the content.
boost::asio::streambuf request;
std::ostream request_stream(&request);
JSON BODY
ptree root, info;
root.put ("some value", "8");
root.put ("message", "value value: value!");
info.put("placeholder", "value");
info.put("value", "daf!");
info.put("module", "value");
root.put_child("exception", info);
std::ostringstream buf;
write_json (buf, root, false);
std::string json = buf.str();
页眉和CONNECTION REQUEST
request_stream << "POST /title/ HTTP/1.1 \r\n";
request_stream << "Host:" << some_host << "\r\n";
request_stream << "User-Agent: C/1.0";
request_stream << "Content-Type: application/json; charset=utf-8 \r\n";
request_stream << json << "\r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Connection: close\r\n\r\n";
// Send the request.
boost::asio::write(socket, request);
我把占位符值,但是如果你看到任何不我的代码,跳出工作,请让我知道我不知道为什么我总是收到400,不好的要求。关于钻机
C++
WIN7
VISUAL STUDIO
在发送整个请求之前,打印到某些日志或控制台并查看它是否正确。 –
你是否在头字段之间写'json'? – PSIAlt
这是json文件输入的地方'request_stream << json <<“\ r \ n”;'我一直在通过调试器和请求缓冲区一起查看request_stream缓冲区。在内容方面一切看起来都是正确的,但是有一些奇怪的空间你认为可能会产生影响? – progrenhard