[记录]java实现curl功能

java实现curl功能

public static String execCurl(String[] cmds){
        ProcessBuilder process = new ProcessBuilder(cmds);
        try {
            Process 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.err.print(e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

示例

String[] cmds = {"curl", "-x", "http://ip代理地址", "http://www.baidu.com"};

execCurl(cmds);

你可能感兴趣的:(Linux,Java)