c++ - BOOST ASIO POST HTTP REQUEST——标题和正文

标签 c++ json http post boost-asio

几天来我一直在努力让它工作,但我一直从服务器收到 400 错误。

基本上,我要做的是向服务器发送一个 http POST 请求,该请求需要一个具有几个属性的 JSON 请求正文。

这些是我目前正在使用的库

已更新 --- 2013 年 7 月 23 日上午 10:00 刚刚注意到我使用的是 TCP 而不是 HTTP,不确定这会对 HTTP 调用产生多大影响,但我找不到任何使用带有 BOOST 的纯 HTTP 的客户端示例::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;

设置代码

    // 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 主体

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();

标题和连接请求

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

最佳答案

虽然这个问题很老,但我想为面临类似 http POST 问题的用户发布这个答案。

服务器向您发送 HTTP 400,意思是“BAD REQUEST”。这是因为您形成请求的方式有点错误。

下面是发送包含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\r\n";
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) 并且要实现这一点(第二点),您必须在最后一个标题中提供双换行符(即\r\n\r\n)您的 header 请求。这告诉 header HTTP 请求内容已经结束,现在它(服务器)将获取数据。

如果您不这样做,那么服务器将无法理解 header 的结束位置? 数据从哪里开始?因此,它一直在等待 promise 的数据(挂起)。

免责声明:如有任何不准确之处,请随时进行编辑。

关于c++ - BOOST ASIO POST HTTP REQUEST——标题和正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17799229/

相关文章:

delphi - EIdOSSLUnderlyingCryptoError 异常

java - 服务请求后停止 HttpServer

node.js - 使用带有 http 分块传输的尾部 header 。如何使用它设置cookie

c++ - ftp 服务器的 makefile - 修改头文件时编译

c++ - Libcurl 停止工作,SSL 连接错误

javascript - Backbone js原始文本发布

wcf - 如何处理从WCF数据服务(OData)返回的json DateTime

c++ - 如何使 QGraphicsTextItem 单行?

c++ - 是否可以从着色器获取数据

java - 无法检索 JSON 值