curl 可以终端请求数据
java实现
package com.sjk.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Create by wyb 2022/1/11 4:36 PM
* description :
*/
public class CurlUtil {
public static String execCurl(String[] cmds) {
ProcessBuilder process = new ProcessBuilder(cmds);
Process p;
try {
p = process.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
builder.append(line);
builder.append(System.getProperty("line.separator"));
}
return builder.toString();
} catch (IOException e) {
System.out.print("error");
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String[] cmds = {
"curl", "-X", "GET", "http://aabb.com/aa/bb/cc",
"-H", "param1: value1",
"-H", "param2: value2"
};
/**
// post
curl -X POST 'http://aabb.com/aa/bb/cc' -H 'Content-Type: application/json' -H 'param1: value1'
-d "{'bodyparam1':'bodyparam2'}"
*/
String resultStr = execCurl(cmds);
}
}