Overview
Features
Download
Documentation
Community
Add-Ons & Services

Poco::Net::SSLConnectionUnexpectedlyClosedException?[SOLVED]

Please post support and help requests here.

Poco::Net::SSLConnectionUnexpectedlyClosedException?[SOLVED]

Postby pulmark » 27 Oct 2012, 19:49

I'm trying to use Poco with openSSL on Windows 7 without success for secure client - server communication. I added the certificate that I received by using openssl console tool for the communication into cacert.pem. Before this when running the program it asked via console to accept the certificate. If I responded yes, program waited for a while and then throw exception.

Now when running the program, no console prompt to accept certificate, but still I do not get response from the server site and then after a while (around 60sec) Poco::Net::SSLConnectionUnexpectedlyClosedException is thrown. So what is the problem ?

I'm not familiar with certificate issues, openSSL configuration or SSL so any help is appreciated. The code below:

Code: Select all
int HTTPSConnection::init(const std::string& userName,
                                        const std::string& password)

       using Poco::Net::Context;

   Poco::SharedPtr<Poco::Net::ConsoleCertificateHandler> pCert =
      new Poco::Net::ConsoleCertificateHandler(false);

   Context::Ptr pContext = new Context(Context::CLIENT_USE, "", "", "C:\\openSSL\\bin\\PEM\\cacert.pem",
      Context::VERIFY_RELAXED, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");

   Poco::Net::SSLManager::instance().initializeClient(0, pCert, pContext);

    try
    {
        Poco::URI uri(URL);

        Poco::Net::HTTPClientSession *client =
         Poco::Net::HTTPSessionFactory::defaultFactory().createClientSession(uri);

        // init request
        Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_1_0);
        std::string day = setRequestContentType(request);
        initRequest(userName, password, PATH_INIT, Poco::Net::HTTPRequest::HTTP_POST, request);

        // build handshake msg
        std::stringstream msg;
        msg << getMessageHeader(day);
        msg << HANDSHAKE;
        msg << getMessageFooter(day);
        int len;
        len = msg.str().length();
   request.setContentLength(len);
   request.write(std::cout);
   std::cout << msg.str();

        // send request + msg using HTTP client
        std::ostream& os = client->sendRequest(request);
        os << msg;
        os.flush();

        // receive response and parse it
        Poco::Net::HTTPResponse response;
   std::ostringstream result;
        std::istream& is = client->receiveResponse(response);
   len = is.gcount();
        Poco::StreamCopier::copyStream(is, result);
        response.write(std::cout);
   std::cout << is;
        if (response.getStatus() == response.HTTP_OK)
        {
            parse(result);
        }
        else
        {
            std::cout << "Login failed to Server\n";
            return 1;
        }
    }
    catch(Poco::Net::ConnectionRefusedException &)
    {
        std::cout << "Connection refused by Server\n";
        return 1;
    }
    catch(Poco::Exception &e)
    {
        std::cout << e.name() << " thrown.\n";
        std::cout << e.message() << "\n";
        return 1;
    }
    catch(std::exception &e)
    {
        std::cout << e.what() << "\n";
        return 1;
    }
    return 0;

pulmark
 
Posts: 5
Joined: 27 Oct 2012, 19:23

Re: Poco::Net::SSLConnectionUnexpectedlyClosedException ?

Postby pulmark » 28 Oct 2012, 08:41

I replaced ostream os write code with os << msg.rdbuf():
Code: Select all
        // send request + msg using HTTP client
        std::ostream& os = client->sendRequest(request);
        os << msg.rdbuf();
        os.flush();


Now I can get the response with status HTTP_OK, but the response body (istream) is empty (it should contain some data) ?

Code: Select all
        Poco::Net::HTTPResponse response;
        std::ostringstream result;
        std::istream& is = client.receiveResponse(response);
        Poco::StreamCopier::copyStream(is, result);
        len = result.str().length();
        response.write(std::cout);
        std::cout << is;
        if (response.getStatus() == response.HTTP_OK)
        {
            parse(result);
        }


What is the correct way to process istream to get response body, code above does not work ?
pulmark
 
Posts: 5
Joined: 27 Oct 2012, 19:23

Re: Poco::Net::SSLConnectionUnexpectedlyClosedException ?

Postby pulmark » 28 Oct 2012, 11:07

So finally succeeded to process response body correct way, used Poco::StreamCopier::copyStreamUnbuffered() to store response body into std::stringstream.

Code: Select all
        Poco::Net::HTTPResponse response;
        std::stringstream result;
        std::istream& is = client.receiveResponse(response);
        Poco::StreamCopier::copyStreamUnbuffered(is, result);
        response.write(std::cout);
        std::cout << result.str().c_str() << std::endl;
        if (response.getStatus() == response.HTTP_OK)
        {
            parse(result);
        }
pulmark
 
Posts: 5
Joined: 27 Oct 2012, 19:23


Return to Support

Who is online

Users browsing this forum: No registered users and 1 guest

cron