0

Good day, I'm trying to call a java servlet from a C++ code. So far I've gotten to this:

            execl( "/usr/bin/lynx", "lynx", "-dump", url.c_str(), (char *) 0);

where "url" is the url encoded string holding the address and parameters.

However I haven't found a way to let execl to return the servlet response in order for me to analyze it within the code. Is there an alternate more efficient way to calling a servlet and handling the answer?

Thank you!

  • To execute a command and capture the output, try this: http://stackoverflow.com/questions/478898/how-to-execute-a-command-and-get-output-of-command-within-c. – abellina Mar 07 '13 at 20:48

1 Answers1

1

You can do it with pipe:

string cmd = "lynx -dump ";
cmd += url;
FILE* pipe = popen(cmd.c_str(), "r");
if (!pipe)
{
    cout << "Couldn't open pipe";
    return;
}
char buffer[128];
string result = "";
while(!feof(pipe)) 
{
    if(fgets(buffer, 128, pipe) != NULL)
        result += buffer;
}
pclose(pipe);
Blood
  • 4,126
  • 3
  • 27
  • 37