2 回答

TA贡献1155条经验 获得超0个赞
您还可以在不使用 curl 的情况下在 Java 中执行帖子(不确定是否必须使用 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.
}
对于发送身体,你可以试试这个。
如果使用 curl 是一项硬性要求,您可以Runtime.getRuntime().exec(command);按此处所示使用,但请注意,这不一定是可移植的(例如,如果未安装 curl),并且如果正在运行的命令未以某种方式验证为防止不良行为者运行任意命令等...
添加回答
举报