Java HTTP Header getServerHeader(URL httpURL)

Here you can find the source of getServerHeader(URL httpURL)

Description

Get the content of the 'Server' header.

License

Open Source License

Parameter

Parameter Description
httpURL a http or https URL to get the header from

Return

the contents of the header or null if anything went wrong or the field was not present.

Declaration

public static String getServerHeader(URL httpURL) 

Method Source Code

//package com.java2s;
/*//  w  w w .j  ava 2s. co  m
 * RHQ Management Platform
 * Copyright (C) 2005-2012 Red Hat, Inc.
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

import java.io.IOException;

import java.net.HttpURLConnection;

import java.net.URL;

public class Main {
    /**
     * Get the content of the 'Server' header.
     *
     * @param httpURL a http or https URL to get the header from
     *
     * @return the contents of the header or null if anything went wrong or the field was not present.
     */
    public static String getServerHeader(URL httpURL) {
        String ret;

        try {
            HttpURLConnection connection = (HttpURLConnection) httpURL
                    .openConnection();
            connection.setRequestMethod("HEAD");
            connection.setConnectTimeout(3000);
            connection.setReadTimeout(1000);

            connection.connect();
            // Get the response code to actually trigger sending the request.
            connection.getResponseCode();
            ret = connection.getHeaderField("Server");
        } catch (IOException e) {
            ret = null;
        }
        return ret;
    }
}

Related

  1. getFromUrl(Map headerMap, URL loc, Proxy proxy)
  2. getHeaderField(final URL url, final String name)
  3. getHttpResponseHeader(HttpURLConnection http)
  4. getIntFromHeader(HttpURLConnection connection, String headerName)
  5. getLastModifiedHeader(URL url)
  6. getURLHeaders(String u)
  7. printHeaderFields(final URL url)
  8. printHttpURLConnectionHeaders(HttpURLConnection httpURLConnection)
  9. readHeadersFromResponse(HttpURLConnection httpURLConnection)