I am executing curl via the following code:
// 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);
}
}
However, when I add the following into the exec string:
I am building the curl string as I go, before I pass it to the method seen above:
if (userAgent.contains(" ")) {
userAgent = " --user-agent '" + Exec.escapeShellString(userAgent) + "' ";
}
With the extra single quotes I get this:
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)'"
Without the single quotes, I get this:
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)"
There is a leading single quote, but not a finishing one. I believe that there should be no single quotes.. anyhow, there is magic somewhere between java and curl...
All I would like to do is pass a string like this:
Opera/9.25 (Windows NT 6.0; U; en)
and expect this:
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)"
Edit:
The reason I am using curl is because curl seems to be the only option that retrieves the content on any response other than 200.301 or 302.
解决方案
I don't know why you are trying to use curl from java when you have a library called Apache httpcleint to handle these things in java.
Take a look at this example.
Alternatively you can also use java's inbuilt URLConnection or HttpURLConnection class for these purposes.
If you are from a PHP background and if you insist on using cURL try libcurl Java bindings.