Example usage for java.net HttpURLConnection getHeaderFields

List of usage examples for java.net HttpURLConnection getHeaderFields

Introduction

In this page you can find the example usage for java.net HttpURLConnection getHeaderFields.

Prototype

public Map<String, List<String>> getHeaderFields() 

Source Link

Document

Returns an unmodifiable Map of the header fields.

Usage

From source file:com.dh.perfectoffer.event.framework.net.network.NetworkConnectionImpl.java

private static void logResBodyAndHeader(HttpURLConnection connection, String body) {
    if (L.canLog(Log.VERBOSE)) {
        L.v(TAG, "Response body: ");

        int pos = 0;
        int bodyLength = body.length();
        while (pos < bodyLength) {
            L.v(TAG, body.substring(pos, Math.min(bodyLength - 1, pos + 200)));
            pos = pos + 200;/*from www . j  a v  a  2 s.co m*/
        }

        L.v(TAG, "Response Header: ");
        Map<String, List<String>> headerFields = connection.getHeaderFields();
        for (Entry<String, List<String>> en : headerFields.entrySet()) {
            L.v(TAG, "key=" + en.getKey() + ",value=" + en.getValue());
        }
    }
}

From source file:com.dh.superxz_bottom.framework.net.network.NetworkConnectionImpl.java

private static void logResBodyAndHeader(HttpURLConnection connection, String body) {
    if (L.canLog(Log.VERBOSE)) {
        L.v(TAG, "Response body: ");

        int pos = 0;
        int bodyLength = body.length();
        while (pos < bodyLength) {
            L.v(TAG, body.substring(pos, Math.min(bodyLength - 1, pos + 200)));
            pos = pos + 200;//from  ww w  .ja  v a2 s .c  o  m
        }

        L.v(TAG, "Response Header: ");
        Map<String, List<String>> headerFields = connection.getHeaderFields();
        for (Map.Entry<String, List<String>> en : headerFields.entrySet()) {
            L.v(TAG, "key=" + en.getKey() + ",value=" + en.getValue());
        }
    }
}

From source file:org.bibsonomy.util.WebUtils.java

/**
 * Returns the cookies returned by the server on accessing the URL. 
 * The format of the returned cookies is as
 * // w  w w .  ja v  a  2  s.com
 * 
 * @param url
 * @return The cookies as string, build by {@link #buildCookieString(List)}.
 * @throws IOException
 */
public static String getCookies(final URL url) throws IOException {
    final HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();

    urlConn.setAllowUserInteraction(false);
    urlConn.setDoInput(true);
    urlConn.setDoOutput(false);
    urlConn.setUseCaches(false);

    urlConn.setRequestProperty(USER_AGENT_HEADER_NAME, USER_AGENT_PROPERTY_VALUE);

    urlConn.connect();

    final List<String> cookies = urlConn.getHeaderFields().get("Set-Cookie");
    urlConn.disconnect();

    return buildCookieString(cookies);
}

From source file:com.adaptris.core.http.client.net.MetadataResponseHeaderImpl.java

@Override
public AdaptrisMessage handle(HttpURLConnection src, AdaptrisMessage msg) {
    addMetadata(src.getHeaderFields(), msg);
    return msg;
}

From source file:org.piwik.ResponseData.java

/**
 * Initialize the local header data with the header fields from the connection.
 * Those information are needed to parse the cookie information.
 * @param connection used to retrieve the header fields
 *//* ww w  . j  a v a2  s  . c o m*/
public ResponseData(final HttpURLConnection connection) {
    headerData = connection.getHeaderFields();
}

From source file:org.jboss.as.test.integration.management.console.WebConsoleRedirectionTestCase.java

@Test
public void testRedirectionInNormalMode() throws Exception {
    final HttpURLConnection connection = getConnection();
    assertEquals(HttpURLConnection.HTTP_MOVED_TEMP, connection.getResponseCode());
    String location = connection.getHeaderFields().get(Headers.LOCATION_STRING).get(0);
    assertEquals("/console/index.html", location);
}

From source file:net.sparkeh.magisterlib.MagisterLib.java

public static String getAuthCookie(String APIUrl, String username, String password) throws Exception {
    URL obj = new URL(APIUrl + "sessie");
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("POST");
    con.setRequestProperty("User-Agent", "Mozilla/5.0");
    con.setRequestProperty("Content-Type", "application/json");
    con.setRequestProperty("Cookie", "SESSION_ID=349c6fcb-f49a-47c4-b129-09158986155b");
    JSONObject o = new JSONObject();
    o.put("GebruikersNaam", username);
    o.put("Wachtwoord", password);
    o.put("IngelogdBlijven", false);
    o.put("GebruikersnaamOnthouden", false);
    con.setDoOutput(true);//  w ww. j  a  v  a2s. c om
    con.getOutputStream().write(o.toJSONString().getBytes("UTF-8"));
    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'POST' request to URL : " + obj.getPath());
    System.out.println("Response Code : " + responseCode);
    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();
    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();
    if (con.getHeaderFields().containsKey("Set-Cookie")) {
        String c = con.getHeaderField("Set-Cookie").toString();
        if (c.contains(";")) {
            String[] s = c.split(";");
            for (String k : s) {
                if (k.contains("SESSION_ID") && k.contains("=")) {
                    return k.split("=")[1].trim();
                }
            }
        }
    }
    return null;
}

From source file:org.jboss.as.test.integration.management.console.WebConsoleRedirectionTestCase.java

@Test
public void testRedirectionInAdminMode() throws Exception {
    ServerReload.executeReloadAndWaitForCompletion(managementClient.getControllerClient(), true);
    try {/*w w  w  .  j ava2  s .  c om*/
        final HttpURLConnection connection = getConnection();
        assertEquals(HttpURLConnection.HTTP_MOVED_TEMP, connection.getResponseCode());
        String location = connection.getHeaderFields().get(Headers.LOCATION_STRING).get(0);
        assertEquals("/consoleerror/noConsoleForAdminModeError.html", location);
    } finally {
        ServerReload.executeReloadAndWaitForCompletion(managementClient.getControllerClient(), false);
    }
}

From source file:com.example.chengcheng.network.httpstacks.HttpUrlConnStack.java

private void addHeadersToResponse(BasicHttpResponse response, HttpURLConnection connection) {
    for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
        if (header.getKey() != null) {
            Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
            response.addHeader(h);/*from   ww w  .  j av a2  s. co  m*/
        }
    }
}

From source file:Main.java

public static String request(String url, Map<String, String> cookies, Map<String, String> parameters)
        throws Exception {
    HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
    connection.setRequestProperty("User-Agent",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36");
    if (cookies != null && !cookies.isEmpty()) {
        StringBuilder cookieHeader = new StringBuilder();
        for (String cookie : cookies.values()) {
            if (cookieHeader.length() > 0) {
                cookieHeader.append(";");
            }//  w w w  .  jav a2  s  .  co m
            cookieHeader.append(cookie);
        }
        connection.setRequestProperty("Cookie", cookieHeader.toString());
    }
    connection.setDoInput(true);
    if (parameters != null && !parameters.isEmpty()) {
        connection.setDoOutput(true);
        OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream());
        osw.write(parametersToWWWFormURLEncoded(parameters));
        osw.flush();
        osw.close();
    }
    if (cookies != null) {
        for (Map.Entry<String, List<String>> headerEntry : connection.getHeaderFields().entrySet()) {
            if (headerEntry != null && headerEntry.getKey() != null
                    && headerEntry.getKey().equalsIgnoreCase("Set-Cookie")) {
                for (String header : headerEntry.getValue()) {
                    for (HttpCookie httpCookie : HttpCookie.parse(header)) {
                        cookies.put(httpCookie.getName(), httpCookie.toString());
                    }
                }
            }
        }
    }
    Reader r = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    StringWriter w = new StringWriter();
    char[] buffer = new char[1024];
    int n = 0;
    while ((n = r.read(buffer)) != -1) {
        w.write(buffer, 0, n);
    }
    r.close();
    return w.toString();
}