将Curl转换为Java等价物
我第一次使用将Curl转换为Java等价物,java,curl,httpurlconnection,Java,Curl,Httpurlconnection,我第一次使用New Relic REST API,我有一个curl命令: 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' 我想在一个Javaservlet中发送这个命令,并从
New Relic REST API
,我有一个curl命令:
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'
我想在一个Javaservlet中发送这个命令,并从响应中获取一个JSON对象,以便进行解析,最好的解决方案是什么
HttpURLConnection
ApacheHttpClient
我尝试了几种不同的解决方案,但到目前为止没有任何效果,我能找到的大多数示例都是使用折旧后的DefaultHttpClient
以下是我尝试的一个例子:
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());
编辑
我对代码做了一些修改,现在可以使用了,谢谢
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();
}
String name=“name[]=EndUser/WebTransaction/WebTransaction/JSP/index.JSP”;
字符串url=”https://api.newrelic.com/v2/applications/myAppId/metrics/data.json";
弦线;
try(PrintWriter=response.getWriter()){
HttpURLConnection conn=(HttpURLConnection)新URL(URL).openConnection();
conn.setRequestProperty(“接受”、“应用程序/json”);
conn.setRequestProperty(“X-Api-Key”、“MyAPI-Key”);
conn.setRequestMethod(“GET”);
连接设置输出(真);
conn.setDoInput(真);
OutputStreamWriter wr=新的OutputStreamWriter(conn.getOutputStream());
wr.写(姓名);
wr.flush();
BufferedReader读取器=新的BufferedReader(新的
InputStreamReader(conn.getInputStream());
而((line=reader.readLine())!=null){
系统输出打印项次(行);
println(HTML\u START+“NewRelic JSON响应:“+line+”+HTML\u END);
}
wr.close();
reader.close();
}捕获(格式错误){
e、 printStackTrace();
}
curl-d
发送您指定的任何内容,而不以任何方式格式化它。只需在OutputStream中发送字符串names[]=EndUser/…
,而不用将其包装到JSONObject中。写入字符串后,不要忘记调用wr.flush()
。当然,在那之后,您需要获取InputStream
并从中开始阅读(我之所以提到这一点,是因为它不在您的代码片段中).并查看详细信息。我建议使用librari,如Apache HttpComponent或Unirest,这大大简化了这一过程。Apache HttpComponent有哪些优点?它是一种易于编码、理解和维护的API。Unirest是建立在Http组件之上的,通过Http连接更容易。很酷,谢谢,我想我会修改我的代码来使用它。