使用POCO上传文件 - SSL连接意外关闭异常

时间:2014-07-16 05:42:19

标签: c++ https httprequest poco poco-libraries

使用POCO将文件上传到HTTPS网址HTTP POST请求始终返回" SSL连接意外关闭"例外

以下是我用于文件的分段上传的代码..

try
{
  Poco::URI uri(uploadLink);
  const Poco::Net::Context::Ptr context = new Poco::Net::Context(Poco::Net::Context::CLIENT_USE, "", "", "", Poco::Net::Context::VERIFY_NONE, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
    Poco::Net::HTTPSClientSession session(uri.getHost(), uri.getPort(), context);
    session.setKeepAlive(true);

  // prepare path
    std::string path(uri.getPathAndQuery());
   if (path.empty())
   {
        path = "/";
   }
  std::cout<<"\nPath: "<<path;


  std::ifstream f1 (filePath,std::fstream::binary);
std::string content((std::istreambuf_iterator<char>(f1)), std::istreambuf_iterator<char>());

std::cout<<"\n Fle Content: "<<content;

std::string boundary = "-------------------------87142694621188";
std::string data1("---------------------------87142694621188\r\nContent-Disposition: form-data; name=\"data\"; filename=\"");
std::string data2(filePath);
std::string data3("\";\r\nContent-Type: application/octet-stream\r\n\r\n"); 
std::string data4("\r\n---------------------------87142694621188--\r\n");

std::string reqBody = data1 +data2 +data3 + content + data4;
std::cout<<"\nReq Body: "<<reqBody.c_str();
Poco::Net::HTTPRequest req(Poco::Net::HTTPRequest::HTTP_POST, path,   Poco::Net::HTTPMessage::HTTP_1_1);
req.setKeepAlive(true);
req.setContentType("multipart/form-data; boundary=-------------------------87142694621188");
req.setContentLength(reqBody.length());

    // sends request, returns open stream
    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();

}
catch (Poco::Exception& e)
{
   std::cout <<"Upload Exception: "<< e.displayText() << std::endl;
}

我也试过Html表格:

Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, path,      Poco::Net::HTTPMessage::HTTP_1_1);
request.setKeepAlive(true);

 request.setContentLength(1041);
Poco::Net::HTMLForm form;
form.setEncoding(Poco::Net::HTMLForm::ENCODING_MULTIPART);

form.addPart("file", new Poco::Net::FilePartSource(filePath));
form.prepareSubmit(request);

session.setTimeout(Poco::Timespan(20, 0));

form.write(session.sendRequest(request));        

Poco::Net::HTTPResponse res;
std::istream &is = session.receiveResponse(res);
Poco::StreamCopier::copyStream(is, std::cout);
  std::cerr << is.rdbuf();

但两种方式都会返回相同的错误。 通过其他平台上传是有效的,所以我可以说它不是服务器问题,但问题出在上面的代码中。请帮我解决这个问题。 注意:服务器不支持分块传输。并且服务器端错误日志表示&#34;错误传输编码:chunked&#34;。虽然我没有进行大块转移。

更新

我终于能够使用第一个代码上传文件(通过设置边界),但是当我尝试从流中读取响应体时,我正在讨论的异常(SSL连接意外关闭)即将到来:

outStringStream&lt;&lt; inStream.rdbuf();

服务器重新调整纯文本。我怎么能得到它?

1 个答案:

答案 0 :(得分:0)

我能够通过逐个字符地读取响应正文来处理异常。以下是使用POCO上传文件的完整代码。

http://developersarea.wordpress.com/2014/10/08/upload-files-using-poco-c-libraries/