Java: URL URLConnection

 

 

 @Test
  public void d() throws IOException{
    URL url = new URL("http://baidu.com");
    InputStream inputStream;
    URLConnection urlConnection = url.openConnection();
    inputStream = urlConnection.getInputStream();
    int len;

    // byte[] bytes = new byte[1024];
    // while((len = inputStream.read(bytes)) != -1){
    //   System.out.println(new String(bytes, 0, len, StandardCharsets.UTF_8));
    // }


    // InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
    //
    // char[] chars = new char[1024];
    //
    // while((len = inputStreamReader.read(chars)) != -1){
    //   System.out.println(new String(chars, 0, len));
    // }
    //
    // inputStreamReader.close();

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
    String line;
    while((line=bufferedReader.readLine())!=null){
      System.out.println("line = " + line);
    }
  }

 

 


 

posted @ 2022-04-12 16:42  ascertain  阅读(31)  评论(0编辑  收藏  举报