简体   繁体   中英

How to send a curl request in java

I'm using a java WebService to send a curl request, this is my request :

curl -d "input = a b c d" 'localhost:8090/jobs?appName=Test40&classPath=fr.aid.cim.spark.JavaWord&sync=true&timeout=1000000000'

I don't know which library I should use and how I can write it in java. Can you please show me how to do it?

If you need execute console command (curl in this case):

Runtime runtime = Runtime.getRuntime();

try {
    Process process = runtime.exec("curl -d \"input = a b c d\" 'localhost:8090/jobs?appName=Test40&classPath=fr.aid.cim.spark.JavaWord&sync=true&timeout=1000000000'");
    int resultCode = process.waitFor();

    if (resultCode == 0) {
        // all is good
    } 
} catch (Throwable cause) {
    // process cause
}

UPDATE (according to your comment): Add folowing lines:

System.out.println("is: " + IOUtils.toString(process.getInputStream()));
System.out.println("es: " + IOUtils.toString(process.getErrorStream()));

What is the output? IOUtils can be found here .

You should take look Apache HttpClient library. With HC Fluent component you can write something like:

Request.Post("http://localhost:8090/jobs?appName=Test40&classPath=fr.aid.cim.spark.JavaWord&sync=true&timeout=1000000000")
.execute().returnContent();

HC Fluent Javadocs

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