简体   繁体   中英

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:

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. The problem comes in when I try to send a POST to my server. Then I get the error message "HTTP Error 400. The request verb is invalid."

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. Google hasn't been much use since post is such an overloaded word online.

Edit - here's an example of my POST. With a GET, the server will pick it up and the handler will complain that it wants a POST (as it should).

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. The POST line should look something like:

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

to be a valid HTTP request. See HTTP/1.1: Request

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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