HttpRequest PUT content in poco library

那年仲夏 提交于 2019-12-05 05:36:23

Quoting the online documentation for HTTPClientSession:

sendRequest() will return an output stream that can be used to send the request body. After you are done sending the request body, create a HTTPResponse object and pass it to receiveResponse().

The following snippet shows one way to use the output stream to read in a file:

try {
    Poco::Net::HTTPClientSession session("www.example.com");
    Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_PUT, "/foo");

    std::ostream& os = session.sendRequest(request);

    std::ifstream ifs("thefile.txt"); // missing: error handling
    Poco::StreamCopier::copyStream(ifs, os); // that's it :-)

    Poco::Net::HTTPResponse response;
    std::istream& rs = session.receiveResponse(response);
    // Do something with rs...

} catch (Poco::Exception& e) {
    std::cout << e.displayText() << std::endl;
}

Also, have a look at the slides for POCO Network programming. They show, among other things, how to use HTTPClientSession.

POCO documentation is terse and to the point; it is worthwhile to read it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!