繁体   English   中英

如何使用Java复制curl命令?

[英]How to replicate curl command using java?

确切地说,我想执行以下curl操作,该操作使用java返回json:

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/streams : java.io.FileNotFoundException: https://api.twitch.tv/helix/streams Response Code : 404

非常感谢所有回复。

快好了! 您正在执行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请求或其他类型的非预期内容将导致请求不匹配。 删除多余的输出内容,然后尝试一下!

您陈述问题的方式有点怪异。 但是我假设您想让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

暂无
暂无

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

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