【问题标题】:How to access CURL command in Java如何在 Java 中访问 CURL 命令
【发布时间】:2018-07-14 11:23:16
【问题描述】:

我正在尝试通过 Java 中的 CURL 访问该链接。

这是我的 CURL 命令:

curl -X POST --insecure --header "Content-Type: application/json" --header "Accept: application/json" -d "{\"searchText\":\"10200597\",\"qf\":\"applId\"}" “https://ped.uspto.gov/api/queries

我尝试使用HttpURLConnection,但它对我不起作用。有没有最好的方法来访问命令?

【问题讨论】:

  • 请查看我们的SO Question Checklist 以帮助您提出一个好问题,从而得到一个好的答案。
  • 您需要提供您使用 HttpURLConnection 尝试过的内容。仅仅因为它对您不起作用并不意味着它不是答案

标签: java url curl


【解决方案1】:

通过使用ProcessBuilder,我们可以解决这个问题:

这里是示例代码:

String[] cmd = {"curl", "-X", "POST", "--insecure", "--header", "Content-Type: application/json", "--header", "Accept: application/json", "-d", "{\"searchText\":\"10200597\",\"qf\":\"applId\"}", "https://ped.uspto.gov/api/queries"};
        ProcessBuilder process = new ProcessBuilder(cmd); 
        Process p;
        try
        {
            p = process.start();
             BufferedReader reader =  new BufferedReader(new InputStreamReader(p.getInputStream()));
                StringBuilder builder = new StringBuilder();
                String line = null;
                while ( (line = reader.readLine()) != null) {
                        builder.append(line);
                        builder.append(System.getProperty("line.separator"));
                }
                String result = builder.toString();
                System.out.print(result);

        }
        catch (IOException e)
        {   System.out.print("error");
            e.printStackTrace();
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-23
    • 2020-01-26
    • 2019-08-17
    • 2015-09-12
    • 1970-01-01
    • 2016-08-15
    • 2014-07-26
    相关资源
    最近更新 更多