在JSP

2013-05-29 28 views
1

執行cURL命令如何在JSP中執行以下cURL命令,在JSP

$ curl -d lang=fr -d badge=0 http://www.redissever.com/subscriber/J8lHY4X1XkU 

如果u一些code.thanks

+0

你爲什麼要這麼做?用Java來做。而不是在JSP中。 –

回答

2

你可以使用Runtime.getRuntime().exec()從執行任何命令解釋是有幫助JSP腳本。

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

我嘗試了Runtime.getRuntime().exec(),但它顯示一個空白頁面 – Sadhana

+0

非常感謝,它正在工作 – Sadhana