public static void main(String[] args) throws IOException { //1.打开浏览器 CloseableHttpClient httpClient = HttpClients.createDefault(); //2.声明get请求 HttpGet httpGet = new HttpGet("http://www.baidu.com/"); //3.发送请求 CloseableHttpResponse response = httpClient.execute(httpGet); //4.判断状态码 if(response.getStatusLine().getStatusCode()==200){ HttpEntity entity = response.getEntity(); //通过EntityUtils工具类转成字符串 String string = EntityUtils.toString(entity, "utf-8"); System.out.println(string); } //5.关闭资源 response.close(); httpClient.close(); }