【问题标题】:cURL request in Java with XGET argument带有 XGET 参数的 Java 中的 cURL 请求
【发布时间】:2016-10-05 11:31:16
【问题描述】:

我喜欢执行类似的 cURL 命令

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

在我的 java 应用程序中(当然是在 linux 机器上运行)并将结果保存在字符串中。 如何正确执行此操作? This 和 this 解决方案不起作用。以下是我迄今为止的尝试和结果,基于解决方案并适应了我的情况:

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

注意:在 shell 中评估命令时,我得到了想要的结果。

【问题讨论】:

  • 试试this问题的第一个答案?
  • 究竟如何?喜欢this?也不行。
  • 请在您的问题中包含您的尝试和结果。外部链接最终会消失,这将使您的问题对未来的读者毫无用处。
  • 请注意,上面显示的 curl 命令行中使用 -X GET 是完全多余的。

标签: java curl


【解决方案1】:

最后我自己找到了解决方案。

首先我缩小了命令:curl 'http://localhost:xxxx/h*/_count'。在这种情况下,它具有相同的结果。

使用以下 java 代码,我能够读取“网页”的内容:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 2015-02-24
    • 1970-01-01
    相关资源
    最近更新 更多