Poco库中的HttpRequest PUT内容 [英] HttpRequest PUT content in poco library
问题描述
我想使用HTTP PUT请求将一些数据从C ++应用程序发送到服务器。我正在使用 poco 库在我的应用程序中进行联网。
I want to send some data from a C++ application to a server using a HTTP PUT request. I am using poco library for networking in my application.
我正在使用此代码段:
HTTPClientSession session(_uri.getHost(), _uri.getPort());
HTTPRequest req(HTTPRequest::HTTP_PUT, path, HTTPMessage::HTTP_1_1);
发送请求时,我在哪里设置内容(文件)流?有人能告诉我一个使用这个库的例子吗?
Where do I set the content (file) stream when I send the request? Can anyone show me an example using this library?
推荐答案
sendRequest()将返回一个输出流可用于发送请求正文。完成发送请求主体后,创建一个HTTPResponse对象并将其传递给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;
}
另外,看看滑动POCO网络编程。除其他外,他们展示了如何使用 HTTPClientSession
。
Also, have a look at the slides for POCO Network programming. They show, among other things, how to use HTTPClientSession
.
POCO文档简洁明了;值得一读。
POCO documentation is terse and to the point; it is worthwhile to read it.
这篇关于Poco库中的HttpRequest PUT内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!