如何在Java中访问CURL命令

如何在Java中访问CURL命令,java,url,curl,Java,Url,Curl,我试图通过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,但它对我

我试图通过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,但它对我无效。有没有访问命令的最佳方法?

通过使用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();
        }

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