Java Utililty Methods HTTP Header

List of utility methods to do HTTP Header

Description

The list of methods to do HTTP Header are organized into topic(s).

Method

voidaddCustomHeaders(HttpURLConnection conn, Map headers)
add Custom Headers
for (Map.Entry<String, String> entry : headers.entrySet()) {
    addHeader(conn, entry.getKey(), entry.getValue());
voidaddHeader(HttpURLConnection conn, String headerName, String value)
add Header
conn.setRequestProperty(headerName, value);
voidaddHeadersToRequest(HttpURLConnection request, Map headers)
add Headers To Request
if (headers != null) {
    for (String key : headers.keySet()) {
        request.addRequestProperty(key, headers.get(key));
voidcopyHttpHeaders(@Nonnull final HttpURLConnection aConn, @Nonnull final InternetHeaders aHeaders)
Copy headers from an Http connection to an InternetHeaders object
for (final Map.Entry<String, List<String>> aConnHeader : aConn.getHeaderFields().entrySet()) {
    final String sHeaderName = aConnHeader.getKey();
    if (sHeaderName != null)
        for (final String sHeaderValue : aConnHeader.getValue()) {
            if (aHeaders.getHeader(sHeaderName) == null)
                aHeaders.setHeader(sHeaderName, sHeaderValue);
            else
                aHeaders.addHeader(sHeaderName, sHeaderValue);
...
HttpURLConnectioncreateHttpConnection(URL endpointUrl, String httpMethod, Map headers)
create Http Connection
try {
    HttpURLConnection connection = (HttpURLConnection) endpointUrl.openConnection();
    connection.setRequestMethod(httpMethod);
    if (headers != null) {
        System.out.println("--------- Request headers ---------");
        for (String headerKey : headers.keySet()) {
            System.out.println(headerKey + ": " + headers.get(headerKey));
            connection.setRequestProperty(headerKey, headers.get(headerKey));
...
Stringdelete(String url, Map headers)
Send a delete request
return fetch("DELETE", url, null, headers);
intdoDelete(URL endpoint, Map headers)
Send
HttpURLConnection urlConnection = null;
int responseCode;
try {
    urlConnection = (HttpURLConnection) endpoint.openConnection();
    try {
        urlConnection.setRequestMethod("DELETE");
    } catch (ProtocolException var33) {
        throw new Exception(
...
voiddumpHeaders(HttpURLConnection conn)
dump Headers
String name;
String value;
for (int i = 0; i < 20; i++) {
    name = conn.getHeaderFieldKey(i);
    value = conn.getHeaderField(i);
    if (name == null && value == null)
        break;
    System.out.println(name + "=" + value);
...
voiddumpHeaders(HttpURLConnection conn, PrintStream out)
dump Headers
for (Map.Entry<String, List<String>> e : conn.getHeaderFields().entrySet())
    for (String v : e.getValue())
        out.println("received header " + e.getKey() + ": " + v);
voiddumpHttpHeaders(HttpURLConnection conn, java.io.PrintStream out)
dump Http Headers
Map<String, List<String>> map = conn.getHeaderFields();
out.println("Printing All Response Header for URL: " + conn.getURL().toString() + "\n");
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
    out.println(entry.getKey() + " : " + entry.getValue());