POCO C ++简单表单提交示例不起作用
问题描述:
我尝试提交POCO库的演示文稿中提供的这个简单表单,但服务器没有收到请求或发布请求。
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);
答
终于在尝试了一下后得到了答案。我在prepareSubmit语句后遗漏了一个form.write语句。我的最终代码就像这样发送post请求以及文件上传请求。
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);
相应的上传服务器使用标准的PHP代码上传HTML表单文件。
The corresponding upload server is using standard PHP code for uploading HTML form files.