BOOST ASIO POST HTTP REQUEST - 标题和正文 [英] BOOST ASIO POST HTTP REQUEST -- headers and body

查看:191
本文介绍了BOOST ASIO POST HTTP REQUEST - 标题和正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图让这个工作几天但是我一直从服务器上得到400错误。

I've been trying to get this to work for a couple of days however I keep getting a 400 error from the server.

基本上,我是什么尝试做的是将http POST请求发送到需要具有几个属性的JSON请求主体的服务器。

Basically, what I'm trying to do is send a http POST request to a server that requires a JSON request body with a couple of properties.

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

These are the libs I'm currently using

更新--- 7/23/13 10: 00am刚刚注意到我正在使用TCP而不是HTTP不确定这将影响HTTP调用但我找不到任何使用纯HTTP和BOOST :: ASIO的客户端示例

UPDATED --- 7/23/13 10:00am just noticed I'm using TCP instead of HTTP not sure how much this will effect an HTTP call but i can't find any examples of clients using pure HTTP with 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;

设置代码

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

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

HEADER AND CONNECTION REQUEST

HEADER AND 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,不好的请求。

I put place holder values however if you see anything that doesn't work in my code that jumps out please let me know I have no idea why i keep getting a 400, bad request.

有关钻机的信息

C ++

WIN7

VISUAL STUDIO

VISUAL STUDIO

推荐答案

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

Although this question is very old I would like to post this answer for users who are facing similar problem for http POST.

服务器向您发送HTTP 400意味着BAD REQUEST。这是因为你形成你的请求的方式有点不对劲。

The server is sending you HTTP 400 means "BAD REQUEST". It is because the way you are forming your request is bit wrong.

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

The following is the correct way to send the POST request containing JSON data.

#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,string等)时,请确保:

Whenever you are sending any data(json,string etc) with your POST request, make sure:

(1) 内容长度是准确的。

(2)您将数据放在请求末尾并带有行间距。

(2) that you put the Data at the end of your request with a line gap.

(3) 并且为此(第二点)发生必须在标头请求的最后一个标头中提供双线馈送(即\\\\\\\ n)。这告诉标题HTTP请求内容已经结束,现在它(服务器)将获取数据。

(3) and for that (2nd point) to happen you MUST provide double line feed (i.e. \r\n\r\n) in the last header of your header request. This tells the header that HTTP request content is over and now it(server) will get the data.

如果你不这样做,那么服务器就无法理解标题结束的位置? 数据的起点在哪里?因此,它一直在等待承诺的数据(它挂起)。

If you don't do this then the server fails to understand that where the header is ending ? and where the data is beginning ? So, it keeps waiting for the promised data (it hangs).

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

Disclaimer: Feel free to edit for inaccuracies, if any.

这篇关于BOOST ASIO POST HTTP REQUEST - 标题和正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆