POCO C ++简单表单提交样本不起作用

时间:2014-02-05 20:48:03

标签: c++ html forms submit poco

我尝试提交POCO库演示文稿中给出的这个简单表格,但服务器没有收到任何获取或发布请求。

HTTPClientSession s("localhost");
HTTPRequest request(HTTPRequest::HTTP_POST, "/fileupload/upload_file.php");
HTMLForm form;
form.add("entry1", "value1");
form.prepareSubmit(request);
s.sendRequest(request);
Poco::Net::HTTPResponse res;
std::istream &is = s.receiveResponse(res);
Poco::StreamCopier::copyStream(is, std::cout);

1 个答案:

答案 0 :(得分:3)

在尝试了一下后终于得到了答案。我在prepareSubmit语句之后缺少一个form.write语句。我的最终代码就是这样发送邮件请求和文件上传请求。

    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表单文件。