Upload a file to a HTTPS url using POCO HTTP POST request always returns "SSL Connection Unexpectedly Closed" Exception
Below is the code i am using for Multipart upload of a file..
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;
}
I also tried with Html forms:
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();
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.
Update:
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:
outStringStream << inStream.rdbuf();
Server is retuning a plain text. How can i get that?