Java执行curl

Java执行curl

 public static void main(String[] args) {
        String[] cmds = {"curl", "-v", "https://api.sandbox.paypal.com/v1/oauth2/token", "-H", "Accept:application/json", "-H", "Accept-Language:en_US", "-u", "AZcQqgjuWGDk55NQ0h1C878c21SxLhUUIBQVV2ZRd5bf9PCgBI2rrCMHeFdStI9IDIEkYoUDpCpFHV79:ECLmfhZgXlDwmwjqMOJz4oDLdg_BIcCZKrpdwCXQKZaEgf0-0MYP7iTFpOJBobMFQLa2L1UWRM1cV2kQ", "-d", "grant_type=client_credentials"};
        ProcessBuilder pb = new ProcessBuilder(cmds);
        pb.redirectErrorStream(true);
        Process p;
        String s = "";
        try {
            p = pb.start();
            BufferedReader br = null;
            String line = null;
            br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            while ((line = br.readLine()) != null) {
                s=line;
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONObject json = JSONObject.fromObject(s);
        String access_token = (String) json.get("access_token");
        System.err.println(access_token);;

    }

你可能感兴趣的:(Java基础,JavaWeb基础)