从c++代码与servlet交互

Interacting with a servlet from a C++ code

本文关键字:servlet 交互 代码 c++      更新时间:2023-10-16

你好,我正在尝试从c++代码调用java servlet。到目前为止,我已经得到了这个:

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

其中"url"是url编码的字符串,包含地址和参数。

然而,我还没有找到一种方法来让execl返回servlet响应,以便我在代码中分析它。是否有一种更有效的方法来调用servlet并处理答案?

谢谢!

您可以使用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);