简体   繁体   English

如何在java中发送curl请求

[英]How to send a curl request in java

I'm using a java WebService to send a curl request, this is my request :我正在使用 java WebService 发送 curl 请求,这是我的请求:

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.我不知道我应该使用哪个库以及如何用 java 编写它。 Can you please show me how to do it?你能告诉我怎么做吗?

If you need execute console command (curl in this case):如果您需要执行控制台命令(在这种情况下为 curl):

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 . IOUtils 可以在这里找到。

You should take look Apache HttpClient library.您应该看看Apache HttpClient库。 With HC Fluent component you can write something like:使用HC Fluent组件,您可以编写如下内容:

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

HC Fluent Javadocs HC Fluent Javadocs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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