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

voidprintHeaderFields(final URL url)
print Header Fields
URLConnection conn = null;
try {
    conn = openConnection(url);
    for (final String name : conn.getHeaderFields().keySet()) {
        System.out.println(name + "\t" + conn.getHeaderField(name));
} catch (Exception e) {
    e.printStackTrace();
...
voidprintHttpURLConnectionHeaders(HttpURLConnection httpURLConnection)
print Http URL Connection Headers
Map<String, List<String>> s = httpURLConnection.getHeaderFields();
Set<Map.Entry<String, List<String>>> set = s.entrySet();
for (Map.Entry<String, List<String>> ss : set) {
    System.out.println("key: " + ss.getKey() + "  value: " + ss.getValue());
MapreadHeadersFromResponse(HttpURLConnection httpURLConnection)
read Headers From Response
Iterator<String> iterator = httpURLConnection.getHeaderFields().keySet().iterator();
Map<String, String> responseHeaders = new HashMap();
while (iterator.hasNext()) {
    String key = iterator.next();
    if (key != null) {
        responseHeaders.put(key, httpURLConnection.getHeaderField(key));
return responseHeaders;
voidsetHeader(final HttpURLConnection couchdbConnection, final String headerName, final String headerValue)
set Header
couchdbConnection.setRequestProperty(headerName, headerValue);
voidsetHeader(HttpURLConnection con, Map headers)
set Header
Iterator iter = headers.keySet().iterator();
while (iter.hasNext()) {
    String key = (String) iter.next();
    con.setRequestProperty(key, (String) headers.get(key));
voidsetHeader(HttpURLConnection urlConnection, String key, String value)
set Header
urlConnection.setRequestProperty(key, value);
voidsetHeaders(HttpURLConnection conn, List headers, String encoding)
set Headers
if (null != headers) {
    for (Iterator<String> iter = headers.iterator(); iter.hasNext();) {
        conn.addRequestProperty(iter.next(), iter.next());
conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + encoding);
voidsetHeadersToRequest(HttpURLConnection urlConnection, Map headersMap)
set Headers To Request
if (headersMap != null && headersMap.size() > 0) {
    Iterator<String> iterator = headersMap.keySet().iterator();
    while (iterator.hasNext()) {
        String key = iterator.next();
        urlConnection.setRequestProperty(key, headersMap.get(key));
StringsubmitHttpRequest(String url, Map headers, String data)
Utility method used to submit an HTTP request.
StringBuilder result = new StringBuilder();
HttpURLConnection connection = null;
try {
    connection = (HttpURLConnection) (new URL(url)).openConnection();
    connection.setUseCaches(false);
    connection.setDoInput(true);
    for (String key : headers.keySet()) {
        connection.setRequestProperty(key, headers.get(key));
...