2013-07-22 49 views
6

我一直试图让这工作几天,但我一直从服务器收到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

+0

在发送整个请求之前,打印到某些日志或控制台并查看它是否正确。 –

+0

你是否在头字段之间写'json'? – PSIAlt

+0

这是json文件输入的地方'request_stream << json <<“\ r \ n”;'我一直在通过调试器和请求缓冲区一起查看request_stream缓冲区。在内容方面一切看起来都是正确的,但是有一些奇怪的空间你认为可能会产生影响? – progrenhard

回答

10

虽然这个问题很老,我想发布这个答案谁正面临着类似的问题对HTTP用户

信息POST。

服务器向您发送HTTP 400的意思是“坏请求”。这是因为你形成你的请求的方式有点不对。

以下是发送包含JSON数据的POST请求的正确方法。

#include<string> //for length() 

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 << "Accept: */*\r\n"; 
request_stream << "Content-Length: " << json.length() << "\r\n";  
request_stream << "Connection: close\r\n\r\n"; //NOTE THE Double line feed 
request_stream << json; 

只要您发送您的POST请求的任何数据(JSON,字符串等),请确保:

(1)的Content-Length:是准确的。

(2)您将数据放在请求的最后,并且存在行间隔。

(3)并为(第2点)发生你MUST在头请求的最后一个头提供双重换行符(即\ r \ n \ r \ n)的。这告诉标头HTTP请求内容已经结束,现在它(服务器)将获得数据。

如果您不这样做,那么服务器无法理解标头的结尾?和数据开始?所以,它一直在等待承诺的数据(它挂起)。

免责声明:如有任何错误,请随时编辑。

+0

+ 1的组合,它工作正常,但服务器端的body在服务器端是空的,具有相同的json数据。我在服务器端使用nodejs。 –