Java URL Connection getHttpHeaders(URLConnection connection)

Here you can find the source of getHttpHeaders(URLConnection connection)

Description

Extract HTTP headers from a connection.

License

Open Source License

Parameter

Parameter Description
connection Connection with HTTP headers

Return

Map of HTTP headers

Declaration

public static Map<String, String> getHttpHeaders(URLConnection connection) 

Method Source Code

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

import java.net.URLConnection;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    /**//from   w w  w .  ja va2s  .c  o m
     * Extract HTTP headers from a connection.
     * @param connection Connection with HTTP headers
     * @return Map of HTTP headers <name, value>
     */
    public static Map<String, String> getHttpHeaders(URLConnection connection) {
        Map<String, String> mapHeaders = new HashMap<>();

        for (Map.Entry<String, List<String>> entries : connection.getHeaderFields().entrySet()) {
            mapHeaders.put(entries.getKey() == null ? "Status code" : entries.getKey(),
                    String.join(",", entries.getValue()));
        }

        return mapHeaders;
    }
}

Related

  1. getGlobalIPAddress(URL automationPage)
  2. getHeaderFieldLong(URLConnection conn, String name, long Default)
  3. getHTML(String url, boolean removeNonLatinChars)
  4. getHTML(URL url)
  5. getHttpGetContent(String strUrl, String charSet)
  6. getHttpResponseHeader(URLConnection http)
  7. getImageFromURL(String remoteURL)
  8. getJar(String jarUrl)
  9. getJarUrlForPackage(String packageName)