Java HTTP Header printHeaderFields(final URL url)

Here you can find the source of printHeaderFields(final URL url)

Description

print Header Fields

License

Open Source License

Declaration

public static void printHeaderFields(final URL url) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;
import java.net.URLConnection;
import java.net.URL;
import java.net.HttpURLConnection;

public class Main {
    public static void printHeaderFields(final URL url) {

        URLConnection conn = null;
        try {//from   ww w  . j a v  a 2 s.  c om
            // Create a URLConnection object for a URL
            conn = openConnection(url);

            for (final String name : conn.getHeaderFields().keySet()) {
                System.out.println(name + "\t" + conn.getHeaderField(name));

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null && conn instanceof HttpURLConnection) {
                ((HttpURLConnection) conn).disconnect();
            }
        }
    }

    private static URLConnection openConnection(final URL url) throws IOException {
        URLConnection conn = url.openConnection();
        conn.setReadTimeout(3000);
        conn.setDefaultUseCaches(false);
        conn.setUseCaches(false);
        return conn;
    }

    public static String getHeaderField(final URL url, final String name) {
        URLConnection conn = null;
        try {
            // Create a URLConnection object for a URL
            conn = openConnection(url);
            return conn.getHeaderField(name);

        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            if (conn != null && conn instanceof HttpURLConnection) {
                ((HttpURLConnection) conn).disconnect();
            }
        }
    }
}

Related

  1. getHttpResponseHeader(HttpURLConnection http)
  2. getIntFromHeader(HttpURLConnection connection, String headerName)
  3. getLastModifiedHeader(URL url)
  4. getServerHeader(URL httpURL)
  5. getURLHeaders(String u)
  6. printHttpURLConnectionHeaders(HttpURLConnection httpURLConnection)
  7. readHeadersFromResponse(HttpURLConnection httpURLConnection)
  8. setHeader(final HttpURLConnection couchdbConnection, final String headerName, final String headerValue)
  9. setHeader(HttpURLConnection con, Map headers)