简体   繁体   中英

How to access CURL command in Java

I am trying to access the link through CURL in Java.

Here is my CURL Command:

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

I tried by using HttpURLConnection , but it didn't worked for me. Is there any best way to access command?

By using ProcessBuilder we can solve this:

Here is sample code:

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();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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