java中执行curl请求模拟http请求

java触发http请求,采用的curl的方式
需要注意的是,命令需要以数组的形式传递参数,就是把正常的命令以空格切分成数组就行了。而且参数前后不能有空格。
linux下命令:

curl  http://192.168.x.xxx:8900/v1 -d '["default","ASDSFGSDGRRGSSSSS"]'

java实现:

		String parm = "\"[\\\"default\\\",\\\"ASDSFGSDGRRGSSSSS\\\"]\"";
    	String[] cmds = {"curl","http://192.168.x.xxx:8900/v1","-d"," "+parm};
        process = new ProcessBuilder(cmds);
       	try {
			p = process.start();
			BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
	        builder = new StringBuilder();
	        String line = null;
	        while ((line = reader.readLine()) != null) {
	            builder.append(line);
	            builder.append(System.getProperty("line.separator"));
	        }
	        return builder;
		} catch (IOException e) {
			e.printStackTrace();
		}
    	return null;

你可能感兴趣的:(java)