简体   繁体   中英

which is the equivalent command for execute a curl command in windows java?

An equivalent command for something like this I don't know which is the correct form to call a batch command

def proc =["/bin/sh",      "-c","curl  https://stackoverflow.com"]

     proc.waitFor()
     StringBuffer outputStream = new StringBuffer()
     proc.waitForProcessOutput(outputStream, System.err)
     String output = outputStream.toString()

Why don't you consider using java.net.URL instead?

Sample code is here

import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Hello{

public static void main(String []args){

try
{
        URL url = new URL("http://stackoverflow.com");
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) {
        for (String line; (line = reader.readLine()) != null;) {
            System.out.println(line);
            }
        }
}
catch (Exception e)
    {
            System.out.println("error occured");
    }
    }
}

instead if you want to invoke the curl command from java use the below

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class ShellFromJava {

    public static ArrayList<String> command(final String cmdline,
    final String directory) {
        try {
            Process process =
                new ProcessBuilder(new String[] {"bash", "-c", cmdline})
                    .redirectErrorStream(true)
                    .directory(new File(directory))
                    .start();

            ArrayList<String> output = new ArrayList<String>();
            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = null;
            while ( (line = br.readLine()) != null )
                output.add(line);

            if (0 != process.waitFor())
                return null;

            return output;

        } catch (Exception e) {
            //Warning: doing this is no good in high-quality applications.
            //Instead, present appropriate error messages to the user.
            //But it's perfectly fine for prototyping.

            return null;
        }
    }

public static void main(String[] args) {
        testHandler("curl http://stackoverflow.com");
    }

    static void testHandler(String cmdline) {
        ArrayList<String> output = command(cmdline, ".");
        if (null == output)
            System.out.println("\n\n\t\tCOMMAND FAILED: " + cmdline);
        else
            for (String line : output)
                System.out.println(line);

    }
}

You can spawn a process from Java:

public class useProcess {
  public static void main(String[] args) throws Exception {
    String params[] = {"/bin/sh", "-c", "curl", "https://stackoverflow.com"};
    Process myProcess = Runtime.getRuntime().exec(params);
    myProcess.waitFor();
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM