java调用curl请求对接接口

1, 原curl调用post接口的命令

curl -H 'Content-Type:application/json' -X POST --data  '{"accountNumber":"[email protected]","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+""};
        //命令的空格在jva数组里单个的,必须分开写,不能有空格,
		String responseMsg=execCurl(cmds);
		System.out.println("curl===curl"+responseMsg);

		return responseMsg;
	}

对接一个中科院的项目,因为网络安全的原因,对方的网络协议在生产上线后变了两次,一次https证书校验,一次TLS 1.0 不安全 给关闭了,还没告知我们他们的网络协议变了,导致java程序两次调不通,防止再变只好用curl的命令了

猜你喜欢

转载自blog.csdn.net/zhaofuqiangmycomm/article/details/108620887