Java HTTP Response getResponseHeader(HttpURLConnection conn)

Here you can find the source of getResponseHeader(HttpURLConnection conn)

Description

get Response Header

License

Apache License

Declaration

public static String getResponseHeader(HttpURLConnection conn) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.net.HttpURLConnection;

public class Main {
    public static String getResponseHeader(HttpURLConnection conn) {
        String headerField = conn.getHeaderField(null);

        if (headerField == null) {
            return null;
        }/* www . j av a2 s .  c  o  m*/
        StringBuffer sb = new StringBuffer();

        sb.append(headerField).append("\r\n");
        String key;
        for (int i = 1; (key = conn.getHeaderFieldKey(i)) != null; i++) {
            if (!key.equals("Transfer-Encoding"))
                sb.append(key).append(": ").append(conn.getHeaderField(i)).append("\r\n");
        }

        return sb.toString();
    }

    public static String getResponseHeader(HttpURLConnection conn, String modifiedKey, String newValue) {
        StringBuffer sb = new StringBuffer();
        sb.append(conn.getHeaderField(null)).append("\r\n");
        String key;
        String value;
        for (int i = 1; (key = conn.getHeaderFieldKey(i)) != null; i++) {
            if (key.equals("Transfer-Encoding"))
                continue;

            if (key.equals(modifiedKey))
                value = newValue;
            else
                value = conn.getHeaderField(i);
            sb.append(key).append(": ").append(value).append("\r\n");
        }

        return sb.toString();
    }
}

Related

  1. getResponseBody(HttpURLConnection conn)
  2. getResponseContent(HttpURLConnection connection)
  3. getResponseContent(HttpURLConnection httpConn)
  4. getResponseContent(String url)
  5. getResponseErrorContent(HttpURLConnection httpConn)
  6. getResponseHeader(String headerName, HttpURLConnection urlConnection)
  7. getResponseHeaders(URLConnection conn, HashMap headers)
  8. getResponseMessage(InputStream inputStream, HttpURLConnection connection)
  9. getResponseStream(final HttpURLConnection connection)