Java HttpURLConnection修改为Apache HttpComponents
我正在修改我的代码以使用ApacheHttpComponents,因为有人告诉我这是一种更干净的方法 HttpURLConnection代码(工作):Java HttpURLConnection修改为Apache HttpComponents,java,apache-httpclient-4.x,Java,Apache Httpclient 4.x,我正在修改我的代码以使用ApacheHttpComponents,因为有人告诉我这是一种更干净的方法 HttpURLConnection代码(工作): String name=“name[]=EndUser/WebTransaction/WebTransaction/JSP/index.JSP”; try(PrintWriter=response.getWriter()){ HttpURLConnection conn=(HttpURLConnection)新URL(URL).openConne
String name=“name[]=EndUser/WebTransaction/WebTransaction/JSP/index.JSP”;
try(PrintWriter=response.getWriter()){
HttpURLConnection conn=(HttpURLConnection)新URL(URL).openConnection();
conn.setRequestProperty(“接受”、“应用程序/json”);
conn.setRequestProperty(“X-Api-Key”、“myId”);
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();
}
这是我修改为使用Apache HttpComponents的代码(404未找到响应):
try(PrintWriter=response.getWriter()){
HttpClient client=HttpClientBuilder.create().build();
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“X-Api-Key”、“myID”);
添加(新的BasicNameValuePair(“names[]”,“EndUser/WebTransaction/WebTransaction/JSP/index.JSP”);
HttpGet request1=新的HttpGet(url+URLEncodedUtils.format(nameValuePairs,“utf-8”);
request1.setHeader(“接受”、“应用程序/json”);
HttpResponse response1=client.execute(request1);
System.out.println(response1.getStatusLine().getStatusCode());
弦线;
BufferedReader reader=新的BufferedReader(新的InputStreamReader(response1.getEntity().getContent());
而((line=reader.readLine())!=null){
系统输出打印项次(行);
println(HTML\u START+“NewRelic JSON响应:“+line+”+HTML\u END);
}
reader.close();
}捕获(格式错误){
e、 printStackTrace();
}
有人能告诉我实现这一点的正确方法吗。更干净的方法是使用类似于库的改进,因为这些都是样板代码 您仍然可以将此代码用作引入Json对象的通用方法,这样您就可以处理这些对象,并从中获取所需的信息。但它并不干净,它很凌乱(相信我):) 由于我没有您实际的API url,我将尝试给出一个使用的示例 改型是类型安全的,这意味着您指定了模型pojo,它将负责对模型本身的Json对象进行必要的强制转换,这很酷 模型
public class Application {
private Integer id;
private String name;
private String language;
private String health_status;
//Getters and setters
}
dto
提问时,包括所有必要的信息。“不工作”对任何相关人员都没有任何帮助。抱歉,我收到了一个未找到的404响应。您可以使用wireshark或类似工具来比较发送的请求是否不同。这是一个好主意,我从未想过要这样做。这不是一个答案,这是一个评论。我可以回答这个问题,然后这样做。建议另一个库不是答案。请给我一个可以实现这一点的改装代码的例子,并解释为什么它是一个更好的方法?@Johntk你想在你的第二个代码片段中写一篇文章吗?
try (PrintWriter writer = response.getWriter()) {
HttpClient client = HttpClientBuilder.create().build();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("X-Api-Key", "myID"));
nameValuePairs.add(new BasicNameValuePair("names[]", "EndUser/WebTransaction/WebTransaction/JSP/index.jsp"));
HttpGet request1 = new HttpGet(url + URLEncodedUtils.format(nameValuePairs, "utf-8"));
request1.setHeader("Accept", "application/json");
HttpResponse response1 = client.execute(request1);
System.out.println(response1.getStatusLine().getStatusCode());
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(response1.getEntity().getContent()));
while ((line = reader.readLine()) != null) {
System.out.println(line);
writer.println(HTML_START + "<h2> NewRelic JSON Response:</h2><h3>" + line + "</h3>" + HTML_END);
}
reader.close();
}catch(MalformedURLException e){
e.printStackTrace();
}
public class Application {
private Integer id;
private String name;
private String language;
private String health_status;
//Getters and setters
}
public class ApplicationListDot {
private List<Application> applications;
}
public interface RestController {
@GET("/v2/applications.json")
ApplicationListDot viewApplications();
}