gpt4 book ai didi

c++ - POCO : How to upload image to webser using poco in c++

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:35:44 27 4
gpt4 key购买 nike

我正在尝试将图像上传到远程网络服务器。我使用过 HTMLForm 和 FilePartSource。我能够成功地将图像上传到本地服务器(即 loclhost),但是当我尝试将其上传到远程服务器时,从远程 Web 服务器收到的响应是“411 Length Required”。我试图设置 request.setContentLength(sizeofimagefile) 但仍然是同样的问题。任何人都可以指导我解决什么问题或。这是我的代码。

    HTMLForm htmlform;
htmlform.set("aaaaaa", "bbbbbbb");
htmlform.set("cccccc", "ddddddd");
htmlform.setEncoding(HTMLForm::ENCODING_MULTIPART);

PartSource * pFileSrc = new FilePartSource("filename", "application/octet-stream");

std::istream& mystream = pFileSrc->stream();
mystream.seekg(0, std::ios::end);
int uiLength = mystream.tellg();

htmlform.addPart("file", pFileSrc);

URI uri("yyy");

HTTPClientSession session(uri.getHost(), uri.getPort());
HTTPRequest post_req(Poco::Net::HTTPRequest::HTTP_POST,"xxx",HTTPMessage::HTTP_1_1);
post_req.setKeepAlive(true);
htmlform.prepareSubmit(post_req);


std::ostream& oustr = session.sendRequest(post_req);
htmlform.write(oustr);

HTTPResponse res;
std::istream& rs = session.receiveResponse(res);

std::cerr << rs.rdbuf();

提前致谢

最佳答案

std::ostream& oustr = session.sendRequest(post_req);
htmlform.write(oustr);

您的代码无法将表单数据分配给请求对象。所以当你调用 session.sendRequest 时,一个空请求被发送到服务器。要正确地将 HTMLForm 转换为 HTTPRequest,您必须这样写 -

htmlform.write(session.sendRequest(post_req));

对我有用的图片上传代码是-

    HTTPRequest request(HTTPRequest::HTTP_POST, "/fileupload/upload_file.php",    HTTPMessage::HTTP_1_1);
HTMLForm form;
form.setEncoding(HTMLForm::ENCODING_MULTIPART);
form.set("entry1", "value1");
form.set("entry2", "value2");
form.addPart("file", new FilePartSource("/home/abc/Pictures/sample.png"));
form.prepareSubmit(request);

HTTPClientSession *httpSession = new HTTPClientSession("localhost");
httpSession->setTimeout(Poco::Timespan(20, 0));
form.write(httpSession->sendRequest(request));

Poco::Net::HTTPResponse res;
std::istream &is = httpSession->receiveResponse(res);
Poco::StreamCopier::copyStream(is, std::cout);

对应的上传服务器使用标准的PHP代码上传HTML表单文件。

关于c++ - POCO : How to upload image to webser using poco in c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13968134/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com