Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ POCO:如何使用c+中的POCO将图像上传到webser+;_C++_Image_Upload_Poco Libraries - Fatal编程技术网

C++ POCO:如何使用c+中的POCO将图像上传到webser+;

C++ POCO:如何使用c+中的POCO将图像上传到webser+;,c++,image,upload,poco-libraries,C++,Image,Upload,Poco Libraries,我正在尝试将图像上载到远程web服务器。我使用了HTMLForm和FilePartSource。我能够成功地将图像上载到本地服务器(即loclhost),但当我尝试在远程服务器中上载图像时,从远程web服务器收到的响应是“411长度必需”。 我试图设置request.setContentLength(sizeofimagefile),但仍然存在相同的问题。 有谁能告诉我这是个什么问题。 这是我的密码 HTMLForm htmlform; htmlform.set("aaaaaa", "b

我正在尝试将图像上载到远程web服务器。我使用了HTMLForm和FilePartSource。我能够成功地将图像上载到本地服务器(即loclhost),但当我尝试在远程服务器中上载图像时,从远程web服务器收到的响应是“411长度必需”。 我试图设置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(); 
htmlformhtmlform;
htmlform.set(“aaaaa”、“bbbbb”);
htmlform.set(“CCCC”、“DDD”);
setEncoding(htmlform::ENCODING\u MULTIPART);
PartSource*pFileSrc=新文件PartSource(“文件名”、“应用程序/八位字节流”);
std::istream&mystream=pFileSrc->stream();
seekg(0,std::ios::end);
int uiLength=mystream.tellg();
addPart(“文件”,pFileSrc);
URI(“yyy”);
HTTPClientSession会话(uri.getHost(),uri.getPort());
HTTPRequest post_req(Poco::Net::HTTPRequest::HTTP_post,“xxx”,HTTPMessage::HTTP_1_1);
post_请求setKeepAlive(真);
htmlform.prepareSubmit(后请求);
std::ostream&oustr=session.sendRequest(post_请求);
htmlform.write(oustr);
HTTPResponse;
std::istream&rs=会话接收方响应(res);

std::cerr如果您可以将文件上载到本地服务器,但无法使用远程服务器,那么首先您应该检查远程Apache/PHP是否有上载限制。在远程服务器中尝试phpinfo()

如果没有,你应该修改你的代码

来自Poco文档,网址为:

HTML格式:

HTMLForm(常量HTTPRequest&request,std::istream& 请求机构)

根据给定的HTTP请求创建HTMLForm上传的文件不可用 默默地丢弃

使用此构造函数:

HTML格式:

HTMLForm(常量HTTPRequest&request,std::istream&requestBody,PartHandler&handler)

根据给定的HTTP请求创建HTMLForm上载的文件将传递给给定的PartHandler

在您的示例中,您应用的构造函数是什么

另一方面,

添加部分:

无效添加部分( 常量std::字符串和名称, PartSource*pSource);将零件/附件(文件上载)添加到表单中。表单获得PartSource的所有权并将其删除 当它不再需要的时候只有在以下情况下才会发送零件: 表单的编码集为“多部分/表单数据”

尝试将“multipart/formdata”与addPart和HTMLForm的第二个构造函数一起使用

如果它不起作用,尝试使用像Wireshark这样的网络嗅探器来检查您发送的内容

检查您的请求的内容长度头是否有sizeof(您的图像)+sizeof(“aaaaaa”和“cccccc”参数)。或者尝试使用GET方法而不是POST发送表单

让我知道它是否有效

问候

std::ostream& oustr = session.sendRequest(post_req);
htmlform.write(oustr);
您的代码无法将表单数据分配到请求对象中。因此,当您调用session.sendRequest时,会向服务器发送一个空请求。要将HTMLForm正确转换为HTTPRequest,必须这样编写-

write(session.sendRequest(post_req))

为我工作的图像上传代码是-

相应的上载服务器正在使用标准PHP代码上载HTML表单文件。

在setContentLength(sizeofimagefile)中,您是否在请求数据块中包含随图像发送的参数“AAAAA”和“cccccc”的大小?如果对表单使用POST方法,则这些参数将转到您尝试上载的图像所在的数据块。
    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);