我编写了两个处理 HTTP 请求的程序。我想知道一个是否比另一个更好-
程序 1(使用 HttpURLConnection)
URL url = new URL("https://www.google.com/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(false);
connection.connect();
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
stringBuilder = new StringBuilder();
程序 2(使用 HttpPost)
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("https://test.com");
HttpResponse httpResponse = httpClient.execute(httpPost);
InputStream inputStream = httpResponse.getEntity().getContent();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
同样在程序 2 中,我使用单例来获取连接对象。但是在程序 1 中没有全局连接对象,每次发出请求时我都需要重新创建 HttpURLConnection 对象。如果我在正确的轨道上,请告诉我。
谢谢你