2 回答

TA貢獻1155條經(jīng)驗 獲得超0個贊
您還可以在不使用 curl 的情況下在 Java 中執(zhí)行帖子(不確定是否必須使用 curl)
摘自這篇文章:
private void doPost() throws Exception {
String url = "http:url/";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
String urlParameters = "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
// could ewually save to file rather than to stdout.
}
對于發(fā)送身體,你可以試試這個。
如果使用 curl 是一項硬性要求,您可以Runtime.getRuntime().exec(command);按此處所示使用,但請注意,這不一定是可移植的(例如,如果未安裝 curl),并且如果正在運行的命令未以某種方式驗證為防止不良行為者運行任意命令等...
添加回答
舉報