POCO C++ simple form submit sample not working

2019-05-22 18:24发布

I tried submitting this simple form given in the presentation of POCO library but the server receives no get or post requests.

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条回答
劫难
2楼-- · 2019-05-22 18:48

Finally got the answer after trying for a bit. I was missing a form.write statement after prepareSubmit statement. My final code goes like this which sends post requests as well as file upload requests.

    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);

The corresponding upload server is using standard PHP code for uploading HTML form files.

查看更多
登录 后发表回答