title: Upload a file using POCO - SSL Connection Unexpectedly Closed Exception
body: <p>Upload a file to a HTTPS url using POCO HTTP POST request always returns "SSL Connection Unexpectedly Closed" Exception</p>
  
  <p>Below is the code i am using for Multipart upload of a file..</p>
  
  <pre><code>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&lt;&lt;"\nPath: "&lt;&lt;path;
  
  
    std::ifstream f1 (filePath,std::fstream::binary);
  std::string content((std::istreambuf_iterator&lt;char&gt;(f1)), std::istreambuf_iterator&lt;char&gt;());
  
  std::cout&lt;&lt;"\n Fle Content: "&lt;&lt;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&lt;&lt;"\nReq Body: "&lt;&lt;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&amp; myOStream = session.sendRequest(req);
      // sends the body
      myOStream &lt;&lt; reqBody;
  
      Poco::Net::HTTPResponse res;
  
      // get the response body from server
      std::istream&amp; inStream = session.receiveResponse(res);
      std::ostringstream outStringStream;
      outStringStream &lt;&lt; inStream.rdbuf();
      std::cout&lt;&lt; outStringStream.str();
  
  }
  catch (Poco::Exception&amp; e)
  {
     std::cout &lt;&lt;"Upload Exception: "&lt;&lt; e.displayText() &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>I also tried with Html forms:</p>
  
  <pre><code>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 &amp;is = session.receiveResponse(res);
  Poco::StreamCopier::copyStream(is, std::cout);
    std::cerr &lt;&lt; is.rdbuf();
  </code></pre>
  
  <p>But both ways returns the same error.
  Upload through other platforms is working, so i can say its not the server issue, but the issue is in the above code. Please help me with the issue. 
  Note: Server does not support chunked transfer. And the server side error log says "Bad transfer encoding: chunked". Although i am not doing chunked transfer.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I am finally able to upload files using the first code(by setting boundary), But the exception i was talking about(SSL connection Unexpectedly Closed) is coming when i try to read the response body from stream using:</p>
  
  <p>outStringStream &lt;&lt; inStream.rdbuf();</p>
  
  <p>Server is retuning a plain text. How can i get that?</p>