0

I am using Poco to send HTTP requests, as the name implies. Right now, I'm just trying to send a GET request to google.com and store the resulting HTML in a string to test out Poco and see if it fits what I need. However, I'm having a little trouble with this. Here's my code:

try 
{
    Poco::URI uri("https://www.google.com");
    std::string path(uri.getPathAndQuery());
    if (path.empty()) path = "/";

    Poco::Net::HTTPSClientSession session(uri.getHost(), uri.getPort());
    Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, path, Poco::Net::HTTPMessage::HTTP_1_1);
    request.add("Content-Length", "0");
    Poco::Net::HTTPResponse response;
    doRequest(session, request, response);
}
catch (Poco::Exception& exc)
{
    OutputDebugStringA(exc.displayText().c_str());
}
void doRequest(Poco::Net::HTTPSClientSession& session, Poco::Net::HTTPRequest& request, Poco::Net::HTTPResponse& response)
{
    session.sendRequest(request);
    std::istream& is = session.receiveResponse(response);
    std::string body(std::istreambuf_iterator<char>(is), { });
    message = (LPCWSTR)response.getStatus();
}

I have put breakpoints on every line of doRequest, but the only ones that are triggered are the first two. After clicking continue on the second, the program simply continues as normal. I'm not getting any exceptions or anything. I am adding Content-Length: 0 to avoid a NoMessageException from Poco.

  • 1
    If you want to read all content of body, this `is >> body` is not sufficient. It reads only first word, until first whitespace. You can read all body to string for example by writing `std::string body(std::istreambuf_iterator(is), { });`. – rafix07 Oct 18 '19 at 05:45
  • You need to use HTTPSClientSession for https. – jignatius Oct 18 '19 at 06:45
  • I changed the code to include both changes but that didn't fix my issue. Now it only runs the first line of doRequest() – WubbaLubbaDubbDubb Oct 18 '19 at 13:44
  • Did you initialise SSL? This article might help: https://stackoverflow.com/questions/10875938/how-to-use-openssl-in-poco-c-library-correctly – jignatius Oct 18 '19 at 14:11
  • Do you know how I could do this in visual studio 2019? – WubbaLubbaDubbDubb Oct 18 '19 at 15:48

0 Answers0