将Curl转换为Java等效项 [英] Convert Curl to Java equivalent

查看:158
本文介绍了将Curl转换为Java等效项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次使用 New Relic REST API ,我有一个curl命令:

I'm working with New Relic REST API for the first time, I have a curl command:

curl -X GET 'https://api.newrelic.com/v2/applications/appid/metrics/data.json' \
     -H 'X-Api-Key:myApiKey' -i \
     -d 'names[]=EndUser/WebTransaction/WebTransaction/JSP/index.jsp' 

我想在Java Servlet中发送此命令,并从响应中获取一个JSON对象以进行解析,最好的解决方案是什么?

I want to send this command in a java servlet and get a JSON object from the response ready for parsing, What is the best solution?

HttpURLConnection?

HttpURLConnection?

Apache httpclient?

Apache httpclient?

我尝试了几种不同的解决方案,但没有解决方案到目前为止,一切正常,我可以找到的大多数示例都使用了已贬值的DefaultHttpClient

I've tried a few different solutions, but nothing has worked so far and most examples I could find are using the depreciated DefaultHttpClient

以下是我尝试之一的示例:

Here is an example of one of my attempts:

 String url = "https://api.newrelic.com/v2/applications.json";
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();

        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("X-Api-Key", "myApiKey");
        conn.setRequestMethod("GET");
        JSONObject names =new JSONObject();

        try {
            names.put("names[]=", "EndUser/WebTransaction/WebTransaction/JSP/index.jsp");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OutputStreamWriter wr= new OutputStreamWriter(conn.getOutputStream());
        wr.write(names.toString());

编辑

我已经稍微修改了代码,现在可以正常工作了。

I've modified the code a bit, it's working now thanks.

String names = "names[]=EndUser/WebTransaction/WebTransaction/JSP/index.jsp";
String url = "https://api.newrelic.com/v2/applications/myAppId/metrics/data.json";
String line;

try (PrintWriter writer = response.getWriter()) {

            HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();

            conn.setRequestProperty("Accept", "application/json");
            conn.setRequestProperty("X-Api-Key", "myApiKey");
            conn.setRequestMethod("GET");
            conn.setDoOutput(true);
            conn.setDoInput(true);

            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(names);
            wr.flush();


            BufferedReader reader = new BufferedReader(new
                    InputStreamReader(conn.getInputStream()));
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
                writer.println(HTML_START + "<h2> NewRelic JSON Response:</h2><h3>" + line + "</h3>" + HTML_END);
            }
            wr.close();
            reader.close();
        }catch(MalformedURLException e){

            e.printStackTrace();
        }


推荐答案

curl -d 发送您指定的任何内容,而不用任何方式对其进行格式化。只需在OutputStream中发送字符串 names [] = EndUser /...,而无需将其包装在JSONObject中。写入字符串后,不要忘记调用 wr.flush()。当然,在那之后,您需要获取 InputStream 并开始阅读它(我只提到这一点,因为它不在您的代码段中)。

curl -d sends whatever you specify without formatting it in any way. Just send the string names[]=EndUser/... in the OutputStream, without wrapping it in a JSONObject. Don't forget to call wr.flush() after writing the string. And of course, after that, you need to get the InputStream and start reading from it (I only mention this because it's not in your snippet).

这篇关于将Curl转换为Java等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆