【发布时间】:2012-02-25 19:21:20
【问题描述】:
我正在通过以下代码执行 curl:
// execute process
Process pr = null;
Runtime run = Runtime.getRuntime();
try {
pr = run.exec(cmdline.split(" "));
A ret = f.f(pr);
pr.waitFor();
return ret;
} catch (Exception ex) {
throw new RuntimeException("Executing " + cmdline, ex);
} finally {
try {
// close all those bloody streams
pr.getErrorStream().close();
pr.getInputStream().close();
pr.getOutputStream().close();
} catch (IOException ex) {
Log.get().exception(Log.Level.Error, "Closing stream: ", ex);
}
}
但是,当我将以下内容添加到 exec 字符串中时:
我正在构建 curl 字符串,然后将它传递给上面看到的方法:
if (userAgent.contains(" ")) {
userAgent = " --user-agent '" + Exec.escapeShellString(userAgent) + "' ";
}
加上额外的单引号,我得到了这个:
113.30.31.137 - - [03/Feb/2012:05:26:39 +0000] "GET / HTTP/1.1" 200 6781 "-" "'Mozilla/5.0(iPad;U;CPUOS3_2_1)'"
没有单引号,我得到这个:
107.21.172.36 - - [03/Feb/2012:05:33:38 +0000] "GET / HTTP/1.1" 200 6781 "-" "'Mozilla/5.0(iPad;U;CPUOS3_2_1)"
有一个前导单引号,但没有一个结束单引号。我相信不应该有单引号..无论如何,java和curl之间有某种魔力......
我想做的就是传递一个这样的字符串: Opera/9.25(Windows NT 6.0;U;en)
并期待这个:
107.21.172.36 - - [03/Feb/2012:05:33:38 +0000] "GET / HTTP/1.1" 200 6781 "-" "Opera/9.25 (Windows NT 6.0; U; en)"
编辑:
我使用 curl 的原因是因为 curl 似乎是在 200.301 或 302 以外的任何响应上检索内容的唯一选项。
【问题讨论】: