如何在 JSP 中执行以下 cURL 命令,
$ curl -d lang=fr -d badge=0 http://www.redissever.com/subscriber/J8lHY4X1XkU
如果你用一些代码解释会很有帮助。谢谢
您可以使用Runtime.getRuntime().exec()
JSP scriptlet 执行任何命令。
<%
Process p=Runtime.getRuntime().exec("...");
p.waitFor();
BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
String line=reader.readLine();
while(line!=null)
{
out.println(line);
line=reader.readLine();
}
%>
但是,如果您可以在纯 Java 中执行外部命令,例如使用HttpUrlConnection
.
<%
URL url = new URL("...");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line=reader.readLine();
while(line!=null)
{
out.println(line);
line=reader.readLine();
}
%>
对于 POST 请求,您需要这样的东西:
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
DataOutputStream wr = new DataOutputStream (connection.getOutputStream ());
wr.writeBytes ("lang=fr&badge=0");
wr.flush ();
wr.close ();