【问题标题】:How to replicate curl command using java?如何使用java复制curl命令?
【发布时间】:2018-12-01 22:25:32
【问题描述】:

准确地说我想在curl下面执行一个返回json和java的动作:

curl -H 'Client-ID: ahh_got_ya' -X GET 'https://api.twitch.tv/helix/streams'

这在 linux shell 中运行良好。

下面是我尝试使用 java json 在 curl 上做的脚本:

{String urly = "https://api.twitch.tv/helix/streams";
    URL obj = new URL(urly);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    con.setRequestMethod("GET");
    con.setRequestProperty("Content-Type","application/json");
    con.setRequestProperty("Client-ID","Ahh_got_ya");


    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes("");
    wr.flush();
    wr.close();

    int responseCode = con.getResponseCode();
    System.out.println("Response Code : " + responseCode);

    BufferedReader iny = new BufferedReader(
    new InputStreamReader(con.getInputStream()));
      String output;
      StringBuffer jsonres = new StringBuffer();

      while ((output = iny.readLine()) != null) {
          jsonres.append(output);
      }
    iny.close();

    //printing result from response
    System.out.println(response.toString());
}

我收到:java.io.FileNotFoundException: https://api.twitch.tv/helix/streamsResponse Code : 404

非常感谢所有回复。

【问题讨论】:

  • 什么是“java json”?
  • @GeorgeJempty 在 java 中使用 json 执行 curl
  • “java json”中的“curl”在哪里
  • @GeorgeJempty curl 在 linux shell 中使用,你可以用它来指定包头、内容等,并做 http GETPOST 的东西。所以问题是:我想制作与上面 curl 命令制作的类似的数据包。
  • 我知道这一切。你的措辞方式完全没有意义。特别是“使用 json”,这与使用 Java 中的 curl 无关。回想起来,我应该投票结束,理由是这“太宽泛”,而不是“不清楚”

标签: java json curl


【解决方案1】:

快到了!您正在执行 GET 调用并且不需要使连接可写——因为您不打算发布。您需要在那里删除该部分。另外 - 要准确了解您的 curl 调用正在做什么,请删除 Content-Type - 因为它没有在 curl 调用中使用。所以你的代码调整应该是:

{
    String urly = "https://api.twitch.tv/helix/streams";
    URL obj = new URL(urly);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    //only 2 headers from cURL call
    con.setRequestMethod("GET");
    con.setRequestProperty("Client-ID","Ahh_got_ya");

    int responseCode = con.getResponseCode();
    System.out.println("Response Code : " + responseCode);

    BufferedReader iny = new BufferedReader(
    new InputStreamReader(con.getInputStream()));
      String output;
      StringBuffer jsonres = new StringBuffer();

      while ((output = iny.readLine()) != null) {
          jsonres.append(output);
      }
    iny.close();

    //printing result from response
    System.out.println(response.toString());
}

出现 404 的原因是您的请求与服务端点所期望的不匹配。发送 POST 请求或其他类型的非预期内容将导致请求不匹配。删除额外的输出内容并试一试!

【讨论】:

    【解决方案2】:

    您提出问题的方式有点奇怪。但我假设您想让 Java 程序对 JSON 文件进行 cURL 调用。现在你的 linux 终端说 BASH 而不是 Java。所以这是第 1 步。

    您必须使用库。
    选项是 java.net.URL 和/或 java.net.URLConnection。

    所以#include 其中一个或其中之一。

    URL url = new URL("https://api.twitch.tv/helix/streams");
    
    try (BufferedReader reader = new BufferedReader(new 
    InputStreamReader(url.openStream(), "UTF-8"))) {
        for (String line; (line = reader.readLine()) != null;) {
            System.out.println(line);
        }
    }
    

    https://docs.oracle.com/javase/tutorial/networking/urls/readingWriting.html

    您可能的意思是您希望 Java 生成 JSON 并通过 Bash 访问 cURL,我不建议任何人这样做。如果你觉得你必须这样做,那就是这样的。

    public class ExecuteShellCommand {
    
    public String executeCommand(String command) {
    

    将字符串设置为 cURL

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-15
      • 2014-07-26
      • 1970-01-01
      相关资源
      最近更新 更多