1

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?

5
  • Have a look at ProcessBuilder. Commented Jan 15, 2015 at 10:39
  • I'd use apache HttpClient Commented Jan 15, 2015 at 10:42
  • There is no such thing as a 'curl request'. Curl is a command line tool to do some form of an online request. Commented Jan 15, 2015 at 10:44
  • Possible duplicate How to use cURL in Java? Commented Jan 15, 2015 at 10:46
  • Ok thank you for your suggestions i will take a lokk. #Reimeus is not a duplication cause i saw it but i could'nt use it with localHost request !! Commented Jan 15, 2015 at 10:50

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer, but my resultCode is allways = 6 ??
3

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

3 Comments

Thank you. But i got this error : java.lang.IllegalArgumentException: Illegal character in scheme name at index 4
Pleace check the uri string that there is no extra characters etx.
Did you get it working, or do you need further help?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.