1, 原curl调用post接口的命令
curl -H 'Content-Type:application/json' -X POST --data '{"accountNumber":"lihuan@psych.ac.cn","externalPlatForm":"CALLCENTER","sign":"97699a609fc025804ad5faebeb85c14c"}' https://www.123.com
2,java 调用curl命令
//java 调用 Curl的方法
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;
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 String getHttpPost(String address, String requestJson) {
String[] cmds = {"curl", "-H", "Content-Type:application/json", "-X","POST","--data",
""+requestJson+"",
""+address+""};
//HTTP请求有两个超时时间:一个是连接超时时间,另一个是数据传输的最大允许时间(请求资源超时时间)。 单位秒
//String[] cmds = {"curl","--connect-timeout","5","m","6", "-H", "Content-Type:application/json", "-X","POST","--data",""+requestJson+"",""+address+""};
//命令的空格在jva数组里单个的,必须分开写,不能有空格,
String responseMsg=execCurl(cmds);
System.out.println("curl===curl"+responseMsg);
return responseMsg;
}
对接一个中科院的项目,因为网络安全的原因,对方的网络协议在生产上线后变了两次,一次https证书校验,一次TLS 1.0 不安全 给关闭了,还没告知我们他们的网络协议变了,导致java程序两次调不通,防止再变只好用curl的命令了
网上有的人说可以设置jdk只接受tls1.2可以解决,感兴趣的小伙伴可以试一下