-3

I like to execute a cURL command like

curl -s -XGET 'http://localhost:xxx/h*/_count?q=*&pretty'

in my java application (which is running of course on a linux-machine) and save the result in a String. How to do this properly? This and this solution don't work. Here are my tries and results until now, based on the solutions and adapted to my case:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;


public class SyncStatus
{

public static void main(String[] args)
{      
    //Link 1   
    Process process;
    try
    {
        process = Runtime.getRuntime().exec( "curl -s -XGET 'http://localhost:xxxx/h*/_count?q=*&pretty'");
        System.out.println(process.waitFor());
        System.out.println(process.getErrorStream());
        System.out.println(process.getInputStream());


        BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String result="";
        String line=null;
        while((line=input.readLine())!=null)
        {
            result+=line;
            System.out.println("here: "+line);
        }
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (InterruptedException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //Results:
    //1 (the Errocode)
    //java.lang.UNIXProcess$ProcessPipeInputStream@2d3584f9
    //java.lang.UNIXProcess$ProcessPipeInputStream@14ad0e9f


    //Link 2
    System.out.println("Next try:");
    List list = new ArrayList();
    list.add("curl");
    list.add("-s");
    list.add("-X");
    list.add("GET");
    list.add("http://localhost:9200/h*/_count?q=*&pretty");
    ProcessBuilder pb = new ProcessBuilder(list);
    try
    {
        pb.redirectErrorStream();
        process = pb.start();
        System.out.println(process.getErrorStream());
        System.out.println(process.getInputStream());
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    //Results:
    //java.lang.UNIXProcess$ProcessPipeInputStream@57dd065c
    //java.lang.UNIXProcess$ProcessPipeInputStream@6fccaf14
}
}

Note: When evaluating the command in a shell, I get the desired result.

4
  • 1
    Try the first answer posted for this question ? Commented Oct 5, 2016 at 11:36
  • how exactly? like this? Doesn't work, too. Commented Oct 5, 2016 at 12:42
  • Please include your tries and results in your question. External links eventually disappear, which will make your question useless to future readers. Commented Oct 5, 2016 at 14:13
  • 1
    Do note that the -X GET use in the curl command line shown above is completely superfluous. Commented Oct 6, 2016 at 6:25

1 Answer 1

0

Finally I got a solution on my own.

First I shrinked the command: curl 'http://localhost:xxxx/h*/_count'. In this case it has the same result.

With the following java-code I was able to read the content of the "webpage":

URL oracle = new URL("http://localhost:9200/h*/_count");
BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));

String inputLine;
while((inputLine = in.readLine()) != null)
    System.out.println(inputLine);
in.close();

Source

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.