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:export.FormCrawler.java

protected void getCookies(HttpURLConnection conn) {
    List<String> connCookies = conn.getHeaderFields().get("Set-Cookie");
    /**/*from w  w w .  j  a  va2s.com*/
     * work-around for weird android 2.2 bug ref
     * http://stackoverflow.com/questions
     * /9134657/nullpointer-exception-with-
     * cookie-on-android-2-2-works-fine-on-2-3-and-above
     */
    if (connCookies == null)
        connCookies = conn.getHeaderFields().get("set-cookie");

    if (connCookies != null) {
        cookies.addAll(connCookies);
    }
}

From source file:com.playhaven.android.req.UrlRequest.java

@Override
public String call() throws MalformedURLException, IOException, PlayHavenException {
    HttpURLConnection connection = (HttpURLConnection) new URL(mInitialUrl).openConnection();
    connection.setInstanceFollowRedirects(true);
    connection.setRequestProperty(CoreProtocolPNames.USER_AGENT, UserAgent.USER_AGENT);
    connection.getContent(); // .getHeaderFields() will return null if headers not accessed once before 

    if (connection.getHeaderFields().containsKey(LOCATION_HEADER)) {
        return connection.getHeaderField(LOCATION_HEADER);
    } else {/*from  w ww.  ja  va2  s  .  c o  m*/
        throw new PlayHavenException();
    }
}

From source file:Fetcher.Fetcher.java

/**
 * This method will extract cookies from {@code connection}
 * ({@link HttpURLConnection}).// ww  w .  java 2 s. co  m
 *
 * @param connection The instance of {@link HttpsURLConnection} which is
 * connected and contains cookie field.
 */
private void getCookies(HttpURLConnection connection) {
    Map<String, List<String>> headerFields = connection.getHeaderFields();
    List<String> cookiesHeader = headerFields.get("Set-Cookie");

    if (cookiesHeader != null) {
        for (String cookie : cookiesHeader) {
            cookies.getCookieStore().add(null, HttpCookie.parse(cookie).get(0));
        }
    }
}

From source file:at.mukprojects.giphy4j.dao.HttpRequestSender.java

private Response send(HttpURLConnection connection) throws IOException {
    connection.connect();// www.j a va  2 s .c  o m

    if (connection.getResponseCode() != 200) {
        throw new IOException("Bad response! Code: " + connection.getResponseCode());
    }

    Map<String, String> headers = new HashMap<String, String>();
    for (String key : connection.getHeaderFields().keySet()) {
        headers.put(key, connection.getHeaderFields().get(key).get(0));
    }

    String body = null;
    InputStream inputStream = null;

    try {
        inputStream = connection.getInputStream();

        String encoding = connection.getContentEncoding();
        encoding = encoding == null ? Giphy4JConstants.ENCODING : encoding;

        body = IOUtils.toString(inputStream, encoding);
    } catch (IOException e) {
        throw new IOException(e);
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
    }

    if (body == null) {
        throw new IOException("Unparseable response body! \n {" + body + "}");
    }

    Response response = new Response(headers, body);

    return response;
}

From source file:org.apache.hadoop.hdfs.ByteRangeInputStream.java

@VisibleForTesting
protected InputStream openInputStream() throws IOException {
    // Use the original url if no resolved url exists, eg. if
    // it's the first time a request is made.
    final boolean resolved = resolvedURL.getURL() != null;
    final URLOpener opener = resolved ? resolvedURL : originalURL;

    final HttpURLConnection connection = opener.connect(startPos, resolved);
    resolvedURL.setURL(getResolvedUrl(connection));

    InputStream in = connection.getInputStream();
    final Map<String, List<String>> headers = connection.getHeaderFields();
    if (isChunkedTransferEncoding(headers)) {
        // file length is not known
        fileLength = null;/*from   ww w .ja  v  a 2s. c  om*/
    } else {
        // for non-chunked transfer-encoding, get content-length
        final String cl = connection.getHeaderField(HttpHeaders.CONTENT_LENGTH);
        if (cl == null) {
            throw new IOException(HttpHeaders.CONTENT_LENGTH + " is missing: " + headers);
        }
        final long streamlength = Long.parseLong(cl);
        fileLength = startPos + streamlength;

        // Java has a bug with >2GB request streams.  It won't bounds check
        // the reads so the transfer blocks until the server times out
        in = new BoundedInputStream(in, streamlength);
    }

    return in;
}

From source file:org.apache.hadoop.yarn.server.webproxy.TestWebAppProxyServlet.java

private boolean isResponseCookiePresent(HttpURLConnection proxyConn, String expectedName,
        String expectedValue) {/* ww w  .ja  v a2 s .co m*/
    Map<String, List<String>> headerFields = proxyConn.getHeaderFields();
    List<String> cookiesHeader = headerFields.get("Set-Cookie");
    if (cookiesHeader != null) {
        for (String cookie : cookiesHeader) {
            HttpCookie c = HttpCookie.parse(cookie).get(0);
            if (c.getName().equals(expectedName) && c.getValue().equals(expectedValue)) {
                return true;
            }
        }
    }
    return false;
}

From source file:com.shopgun.android.sdk.network.impl.HttpURLNetwork.java

private HttpResponse getHttpResponse(HttpURLConnection connection) throws IOException {

    ProtocolVersion pv = new ProtocolVersion("HTTP", 1, 1);
    String rm = connection.getResponseMessage();
    HttpResponse response = new BasicHttpResponse(pv, connection.getResponseCode(), rm);

    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   w  w w .ja va 2  s  . c  o m
        }
    }

    return response;
}

From source file:org.okj.im.core.service.QQHttpService.java

/**
 * ??httpclient?/*from w w w  .j a v a 2s. c o  m*/
 * @param url
 * @param method
 * @param exParam
 * @return
 */
public String sendHttpMessage(String url, String method, ExParam exParam) {
    if (exParam == null) {
        exParam = new ExParam();
    }
    HttpURLConnection conn = null;
    do {
        conn = connect(url, method, exParam); //
    } while (!checkResponseCode(conn));

    //?cookies
    if (conn.getHeaderFields().get("Set-Cookie") != null) {
        StringBuffer cookies = new StringBuffer();
        for (String s : conn.getHeaderFields().get("Set-Cookie")) {
            cookies.append(s);
        }
        exParam.setCookie(cookies.toString());
    }

    if (StringUtils.equalsIgnoreCase("gzip", conn.getHeaderField("Content-Encoding"))) {
        return getGzipResponseString(conn);
    }
    return getResponseString(conn);
}

From source file:org.apache.hadoop.hdfs.web.ByteRangeInputStream.java

@VisibleForTesting
protected InputStream openInputStream() throws IOException {
    // Use the original url if no resolved url exists, eg. if
    // it's the first time a request is made.
    final boolean resolved = resolvedURL.getURL() != null;
    final URLOpener opener = resolved ? resolvedURL : originalURL;

    final HttpURLConnection connection = opener.connect(startPos, resolved);
    resolvedURL.setURL(getResolvedUrl(connection));

    InputStream in = connection.getInputStream();
    final Map<String, List<String>> headers = connection.getHeaderFields();
    if (isChunkedTransferEncoding(headers)) {
        // file length is not known
        fileLength = null;/*from  w  ww.j a  va 2s  .  c  o  m*/
    } else {
        // for non-chunked transfer-encoding, get content-length
        long streamlength = getStreamLength(connection, headers);
        fileLength = startPos + streamlength;

        // Java has a bug with >2GB request streams.  It won't bounds check
        // the reads so the transfer blocks until the server times out
        in = new BoundedInputStream(in, streamlength);
    }

    return in;
}

From source file:com.amgems.uwschedule.api.uw.LoginAuthenticator.java

/**
 * Collects all cookies from a given response source into a list of NameValuePairs.
 *///from ww  w.  ja v  a2 s  .c  o m
private List<String> getAuthCookies(HttpURLConnection connection, BufferedWriter writer,
        List<? extends NameValuePair> postParams) throws IOException {
    writer.write(NetUtils.toQueryString(postParams));
    writer.flush();
    return connection.getHeaderFields().get("Set-Cookie");
}