java 如何用java发送curl

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35908918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:44:11  来源:igfitidea点击:

How to send curl with java

javacurl

提问by Irshad Qureshi

I need to send parameter in curl requestbut i don't about curl request.

我需要发送参数,curl request但我不关心 curl 请求。

Request is: How to send it in java.

请求是:如何在java中发送。

curl --user xxx:xxxpass-H "Content-Type: application/json" -X POST -d
'{"ImageId":"local",
  "ImageUrl":"http://www.gapcanada.ca/products/res/thumbimg/classic-fit-and-flare-dress-modern-red.jpg"}'

http://000.00.00.00:8080/api

回答by Aaron Esau

Well, there are better ways of doing this, but if for some reason you really want to use curl,

嗯,有更好的方法可以做到这一点,但是如果出于某种原因你真的想使用 curl,

Runtime.getRuntime.exec("curl --user xxx:xxxpass-H \"Content-Type: 
    application/json\" -X POST -d
    \'{\"ImageId\":\"local\",
    \"ImageUrl\":\"http://www.gapcanada.ca/products/res/thumbimg/classic-fit-and-flare-dress-modern-red.jpg\"}\'"

Essentially, this Java function runs a command in terminal, so you can execute your request.

本质上,此 Java 函数在终端中运行命令,因此您可以执行您的请求。

That is, for a post or put request. For get, just read it's output.

也就是说,对于 post 或 put 请求。对于获取,只需阅读它的输出。

http://000.00.00.00:8080/api

http://000.00.00.00:8080/api

If you want to send the request without having to install curl executables on the client system, look at Apache HttpClient library. :)

如果您想发送请求而不必在客户端系统上安装 curl 可执行文件,请查看 Apache HttpClient 库。:)

回答by close2

If I understand you correctly, you want to consume the API and somebody gave you the curl command as an example how to do so.

如果我理解正确,您想使用 API 并且有人给了您 curl 命令作为示例如何这样做。

You now want to achieve the same in your JAVA program.

您现在希望在您的 JAVA 程序中实现相同的目标。

There are a few libraries which allow you to achieve this. Google for ?java http client post?.

有一些库可以让您实现这一目标。Google for ?java http client post?。

Sending HTTP POST Request In Javaor HTTP POST using JSON in Javaanswers this is as well.

Sending HTTP POST Request In JavaHTTP POST using JSON in Java答案也是如此。

In your case this would be something like:

在您的情况下,这将类似于:

JSONObject json = new JSONObject();
json.put("ImageId", "local");    
json.put("ImageUrl", "http://www.gapcanada.ca/products/res/thumbimg/classic-fit-and-flare-dress-modern-red.jpg");    

CloseableHttpClient httpClient = HttpClientBuilder.create().build();

try {
    HttpPost request = new HttpPost("http://000.00.00.00:8080/api");
    StringEntity params = new StringEntity(json.toString());
    request.addHeader("content-type", "application/json");
    request.setEntity(params);
    httpClient.execute(request);
// handle response here...
} catch (Exception ex) {
    // handle exception here
} finally {
    httpClient.close();
}