如何在java中发送curl请求

xxe27gdn  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(529)

我正在使用java Web服务发送一个curl请求,这是我的请求:

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

我不知道我应该用哪个lebrary,如何用java编写它。你能告诉我怎么做吗。
谢谢您。

aemubtdh

aemubtdh1#

您应该看看apachehttpclient库。使用hc fluent组件,您可以编写如下内容:

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

hc fluent Java文档

fykwrbwg

fykwrbwg2#

如果需要执行console命令(本例中为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
}

更新(根据您的评论):添加以下行:

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

输出是什么?在这里可以找到很多。

相关问题