2013-03-07 81 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