-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.

Community
  • 1
  • 1
MUmla
  • 445
  • 1
  • 8
  • 26
  • 1
    Try the first answer posted for [this](http://stackoverflow.com/questions/29110716/how-do-i-do-the-following-curl-command-in-java) question ? – lavina Oct 05 '16 at 11:36
  • how exactly? like [this](http://pastebin.com/3dEkLDCs)? Doesn't work, too. – MUmla Oct 05 '16 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. – VGR Oct 05 '16 at 14:13
  • 1
    Do note that the [-X GET](https://stackoverflow.com/questions/8498371/curl-get-and-x-get/8502004#8502004) use in the curl command line shown above is completely superfluous. – Daniel Stenberg Oct 06 '16 at 06:25

1 Answers1

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

MUmla
  • 445
  • 1
  • 8
  • 26