Ich mag wieAnfrage Curl in Java mit XGET Argumente
curl -s -XGET 'http://localhost:xxx/h*/_count?q=*&pretty'
in meiner Java-Anwendung einen cURL-Befehl auszuführen (das auf einer Linux-Maschine natürlich läuft) und das Ergebnis in einem String speichern. Wie mache ich das richtig? This und this Lösung funktionieren nicht. Hier sind meine Versuche und Ergebnisse bisher auf der Grundlage der Lösungen und angepasst meinem Fall:
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)
//[email protected]
//[email protected]
//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:
//[email protected]
//[email protected]
}
}
Hinweis: Wenn Sie den Befehl in einer Shell Auswertung, bekomme ich das gewünschte Ergebnis.
die erste Antwort Versuchen geschrieben für [diese] (http: // Stackoverflow. com/questions/29110716/how-do-i-die-folgende-curl-command-in-java) Frage? – lavina
Wie genau? wie [das] (http://pastebin.com/3dEkLDCs)? Funktioniert auch nicht. – MUmla
Bitte fügen Sie Ihre Versuche und Ergebnisse in Ihre Frage ein. Externe Links verschwinden schließlich, was Ihre Frage für zukünftige Leser nutzlos macht. – VGR