简体   繁体   English

如何使用boost :: asio :: ip :: tcp :: iostream发送POST?

[英]How do I send a POST with boost::asio::ip::tcp::iostream?

From the Boost docs , you can send a GET through an iostream quite easily: Boost docs中 ,您可以很容易地通过iostream发送GET:

ip::tcp::iostream stream;
stream.expires_from_now(boost::posix_time::seconds(60));
stream.connect("www.boost.org", "http");
stream << "GET /LICENSE_1_0.txt HTTP/1.0\r\n";
stream << "Host: www.boost.org\r\n";
stream << "Accept: */*\r\n";
stream << "Connection: close\r\n\r\n";
stream.flush();
std::cout << stream.rdbuf();

When I modify the above to connect to my IIS server, it works fine. 当我修改以上内容以连接到IIS服务器时,它可以正常工作。 The problem comes in when I try to send a POST to my server. 当我尝试向服务器发送POST时出现问题。 Then I get the error message "HTTP Error 400. The request verb is invalid." 然后,我收到错误消息“ HTTP错误400。请求谓词无效。”

Various online discussions make it seem that the problem is with separator characters in headers, but removing all question marks fixed nothing. 各种在线讨论使问题似乎出在标题中的分隔符上,但是删除所有问号并不能解决任何问题。

Is there something that I'm missing here? 我在这里缺少什么吗? This forum discussion makes it look like POSTing with an iostream should be doable. 这个论坛讨论使使用iostream POST看起来应该可行。 Google hasn't been much use since post is such an overloaded word online. Google并没有太多使用,因为post在网上是一个超载的单词。

Edit - here's an example of my POST. 编辑-这是我的POST的示例。 With a GET, the server will pick it up and the handler will complain that it wants a POST (as it should). 使用GET,服务器将对其进行拾取,并且处理程序将抱怨它想要POST(应该这样做)。

boost::asio::ip::tcp::iostream stream;

stream.connect("myurl.com", "http");
stream << "POST /.api/api.svc/objects/723aa707-4978-4062-bcc6-67b05783c4ec/comments/add\r\n";
stream << "Host: myurl.com\r\n";
stream << "Accept: */*\r\n";
stream << "Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n";
stream << "Content-Length: 51\r\n";
stream << "Connection: close\r\n\r\n";

stream << "message=%3Cp%3EHello%3C%2Fp%3E";
stream.flush();
std::cout << stream.rdbuf();

As @Omaha spotted, the POST request is invalid. 如@Omaha所示,POST请求无效。 The POST line should look something like: POST行应类似于:

stream << "POST /.api/api.svc/objects/723aa707-4978-4062-bcc6-67b05783c4ec/comments/add  HTTP/1.0\r\n"

to be a valid HTTP request. 是有效的HTTP请求。 See HTTP/1.1: Request 参见HTTP / 1.1:请求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM