简体   繁体   English

读取POCO HTTPClientRequest的响应

[英]Read Response of POCO HTTPClientRequest

Hi I am uploading a file using multipart upload in POCO (by sending a the content of file appended with boundaries through a normal POST request). 嗨,我正在POCO中使用分段上传来上传文件(通过普通的POST请求发送带有边界的文件内容)。 But when I try to read the response as: 但是当我尝试读取响应为:

std::ostream& myOStream = session.sendRequest(req);
// sends the body
myOStream << reqBody;

Poco::Net::HTTPResponse res;

// get the response body from server
std::istream& inStream = session.receiveResponse(res);
std::ostringstream outStringStream;
outStringStream << inStream.rdbuf();
std::cout<< outStringStream.str();

I get this exception "SSL Connection Unexpectedly Closed" on this line: 我在此行上收到此异常“ SSL连接意外关闭”:

outStringStream << inStream.rdbuf();

Also if i instead try to read the response as: 另外,如果我改为尝试读取响应为:

Poco::StreamCopier::copyStream( inStream, outStringStream );
std::cout<< outStringStream.str();

I get empty response (No exception in this case). 我得到的响应是空的(在这种情况下也不例外)。 The server returns a plain text in response. 服务器返回纯文本作为响应。 And i am getting it as response if using qt, but not through POCO. 如果使用qt而不是通过POCO,我将其作为响应。 Please help me to read the response body. 请帮助我阅读回复正文。 What could be the reason of this exception?? 发生此异常的原因可能是什么?

Also, one more thing if I read the response status as res.getStatus() and res.getReason() it returns 200Ok. 另外,如果我将响应状态读取为res.getStatus()和res.getReason(),则返回200Ok。 I dont understand what is the issue with response body. 我不了解响应正文有什么问题。

This code is working for me you can try. 这段代码对我有用,您可以尝试。

std::istream& rs = _httpClientSession.receiveResponse(response);
memset(_response, 0, RESPONSE_BUFF_SIZE);
rs.read(_response, RESPONSE_BUFF_SIZE-1);
//below code is just to clear the istream
std::streamsize nRead = rs.gcount();
while( !rs.eof() || nRead != 0)
{
    rs.read(_response, RESPONSE_BUFF_SIZE);
    nRead = rs.gcount();
}

One more thing, in case of any exception you have to reset http session by invoking _httpClientSession.reset(); 还有一件事,万一发生任何异常,您必须通过调用_httpClientSession.reset()来重置http会话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM