dimanche 3 mars 2019

Poco::Net::HTTPClientSession throw exception without unique_ptr

I have 2 version of code:

    // HTTP.h
    class HTTP
    {
    private:
      //unique_ptr<HTTPClientSession> session;
      HTTPClientSession session;

      bool doRequest(Poco::Net::HTTPRequest& request, const string& path, const string& content, string& responseBody);
    public:
      HTTP(const string& adress);
    }

    // HTTP.cpp

    HTTP::HTTP(const string& adress)
    {
      Poco::URI uri(
        adress.substr(0, 4) == "http" ? adress : "http://" + adress
      );
      //session = make_unique<HTTPClientSession>(uri.getHost(), uri.getPort());
      HTTPClientSession session{ uri.getHost(), uri.getPort() };
    }

    bool HTTP::doRequest(HTTPRequest& request, const string& path, const string& content, string& responseBody) {
      ...
      session.setTimeout(Timespan(5, 0));
      //std::ostream& requestStream = session->sendRequest(request);
      std::ostream& requestStream = session.sendRequest(request);
      ...
    }

If I use unique_ptr to create session object, like in commented code, everythings worls fine.

But if I use just simple variables, like in current version. Then line

session.sendRequest(request)

throw exception Connection refused

I do not understand why? Could you explein me please.

P.S. request it is simple HTTP GET request to static resource.

Aucun commentaire:

Enregistrer un commentaire