Esempio n. 1
0
  public void fun(URLConnection connection) {
    try {
      connection.connect();
      // print header fields

      Map<String, List<String>> headers = connection.getHeaderFields();
      for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
        String key = entry.getKey();
        for (String value : entry.getValue()) System.out.println(key + ": " + value);
      }

      // print convenience functions

      System.out.println("----------");
      System.out.println("getContentType: " + connection.getContentType());
      System.out.println("getContentLength: " + connection.getContentLength());
      System.out.println("getContentEncoding: " + connection.getContentEncoding());
      System.out.println("getDate: " + connection.getDate());
      System.out.println("getExpiration: " + connection.getExpiration());
      System.out.println("getLastModifed: " + connection.getLastModified());
      System.out.println("----------");

      Scanner in = new Scanner(connection.getInputStream());

      // print first ten lines of contents

      for (int n = 1; in.hasNextLine() && n <= 10; n++) System.out.println(in.nextLine());
      if (in.hasNextLine()) System.out.println(". . .");
    } catch (IOException e) {
      e.printStackTrace();
    }
  }