2013-03-07 83 views
0

美好的一天,我想从C++代码中调用一个java servlet。到目前为止,我已经得到这个:从C++代码与servlet交互

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

其中“url”是url编码的字符串保存的地址和参数。

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

谢谢!

+0

要执行命令并捕获输出,试试这个:http://stackoverflow.com/questions/478898/how-to-execute-a-command -and-GET-输出的命令-内-C。 – abellina 2013-03-07 20:48:44

回答

1

你可以用管道做到这一点:

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); 
+0

非常好,thnx! – 2013-03-11 17:44:07