Example usage for java.net HttpURLConnection getHeaderField

List of usage examples for java.net HttpURLConnection getHeaderField

Introduction

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

Prototype

public String getHeaderField(int n) 

Source Link

Document

Returns the value for the n th header field.

Usage

From source file:vocab.VocabUtils.java

/**
 * Jena fails to load models in https with content negotiation. Therefore I do
 * the negotiation here directly/* ww w. ja  v a 2s . co  m*/
 */
private static void doContentNegotiation(OntModel model, Vocabulary v, String accept, String serialization) {
    try {
        model.read(v.getUri(), null, serialization);

    } catch (Exception e) {
        try {
            System.out.println("Failed to read the ontology. Doing content negotiation");
            URL url = new URL(v.getUri());
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("Accept", accept);

            int status = connection.getResponseCode();
            if (status == HttpURLConnection.HTTP_SEE_OTHER || status == HttpURLConnection.HTTP_MOVED_TEMP
                    || status == HttpURLConnection.HTTP_MOVED_PERM) {
                String newUrl = connection.getHeaderField("Location");
                //v.setUri(newUrl);
                connection = (HttpURLConnection) new URL(newUrl).openConnection();
                connection.setRequestProperty("Accept", accept);
                InputStream in = (InputStream) connection.getInputStream();
                model.read(in, null, serialization);
            }
        } catch (Exception e2) {
            System.out.println("Failed to read the ontology");
        }
    }
}

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

/**
 * Two-step Create/Append:/* w  ww.  jav  a  2  s.  c o m*/
 * Step 1) Submit a Http request with neither auto-redirect nor data. 
 * Step 2) Submit another Http request with the URL from the Location header with data.
 * 
 * The reason of having two-step create/append is for preventing clients to
 * send out the data before the redirect. This issue is addressed by the
 * "Expect: 100-continue" header in HTTP/1.1; see RFC 2616, Section 8.2.3.
 * Unfortunately, there are software library bugs (e.g. Jetty 6 http server
 * and Java 6 http client), which do not correctly implement "Expect:
 * 100-continue". The two-step create/append is a temporary workaround for
 * the software library bugs.
 */
static HttpURLConnection twoStepWrite(HttpURLConnection conn, final HttpOpParam.Op op) throws IOException {
    //Step 1) Submit a Http request with neither auto-redirect nor data. 
    conn.setInstanceFollowRedirects(false);
    conn.setDoOutput(false);
    conn.connect();
    validateResponse(HttpOpParam.TemporaryRedirectOp.valueOf(op), conn);
    final String redirect = conn.getHeaderField("Location");
    conn.disconnect();

    //Step 2) Submit another Http request with the URL from the Location header with data.
    conn = (HttpURLConnection) new URL(redirect).openConnection();
    conn.setRequestMethod(op.getType().toString());
    return conn;
}

From source file:com.francetelecom.clara.cloud.paas.it.services.helper.PaasServicesEnvApplicationAccessHelper.java

/**
 * Check if a string appear in the html page
 *
 * @param url//from   w  w  w  . j  a v a2 s  . c om
 *            URL to test
 * @param token
 *            String that must be in html page
 * @return Cookie that identifies the was or null if the test failed. An
 *         empty string means that no cookie was found in the request, but
 *         the check succeeded.
 */
private static String checkStringAndReturnCookie(URL url, String token, String httpProxyHost,
        int httpProxyPort) {
    InputStream is = null;
    String cookie = null;
    try {
        HttpURLConnection connection;
        if (httpProxyHost != null) {
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(httpProxyHost, httpProxyPort));
            connection = (HttpURLConnection) url.openConnection(proxy);
        } else {
            connection = (HttpURLConnection) url.openConnection();
        }
        connection.setRequestMethod("GET");

        is = connection.getInputStream();
        // check http status code
        if (connection.getResponseCode() == 200) {
            DataInputStream dis = new DataInputStream(new BufferedInputStream(is));
            StringWriter writer = new StringWriter();
            IOUtils.copy(dis, writer, "UTF-8");
            if (writer.toString().contains(token)) {
                cookie = connection.getHeaderField("Set-Cookie");
                if (cookie == null)
                    cookie = "";
            } else {
                logger.info("URL " + url.getFile() + " returned code 200 but does not contain keyword '" + token
                        + "'");
                logger.debug("1000 first chars of response body: " + writer.toString().substring(0, 1000));
            }
        } else {
            logger.error("URL " + url.getFile() + " returned code " + connection.getResponseCode() + " : "
                    + connection.getResponseMessage());
            if (System.getProperty("http.proxyHost") != null) {
                logger.info("Using proxy=" + System.getProperty("http.proxyHost") + ":"
                        + System.getProperty("http.proxyPort"));
            }
        }
    } catch (IOException e) {
        logger.error("URL test failed: " + url.getFile() + " => " + e.getMessage() + " ("
                + e.getClass().getName() + ")");
        if (System.getProperty("http.proxyHost") != null) {
            logger.info("Using proxy=" + System.getProperty("http.proxyHost") + ":"
                    + System.getProperty("http.proxyPort"));
        }
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (IOException ioe) {
            // just going to ignore this one
        }
    }
    return cookie;

}

From source file:nl.ordina.bag.etl.mail.handler.HttpClient.java

private String getFilename(HttpURLConnection connection) {
    String s = connection.getHeaderField("Content-Disposition");
    Matcher matcher = filePattern.matcher(s);
    return matcher.find() ? matcher.group(1) : UUID.randomUUID().toString();
}

From source file:com.gson.util.HttpKit.java

/**
 * ?/*from w w  w  .  ja  v a2 s.  c  o m*/
 * @param url
 * @return
 * @throws IOException
 */
public static Attachment download(String url) throws IOException {
    Attachment att = new Attachment();
    HttpURLConnection conn = initHttp(url, _GET, null);
    if (conn.getContentType().equalsIgnoreCase("text/plain")) {
        // BufferedReader???URL?  
        InputStream in = conn.getInputStream();
        BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));
        String valueString = null;
        StringBuffer bufferRes = new StringBuffer();
        while ((valueString = read.readLine()) != null) {
            bufferRes.append(valueString);
        }
        in.close();
        att.setError(bufferRes.toString());
    } else {
        BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
        String ds = conn.getHeaderField("Content-disposition");
        String fullName = ds.substring(ds.indexOf("filename=\"") + 10, ds.length() - 1);
        String relName = fullName.substring(0, fullName.lastIndexOf("."));
        String suffix = fullName.substring(relName.length() + 1);

        att.setFullName(fullName);
        att.setFileName(relName);
        att.setSuffix(suffix);
        att.setContentLength(conn.getHeaderField("Content-Length"));
        att.setContentType(conn.getHeaderField("Content-Type"));

        att.setFileStream(bis);
    }
    return att;
}

From source file:com.github.codingtogenomic.CodingToGenomic.java

private static String getContent(final String endpoint)
        throws MalformedURLException, IOException, InterruptedException {

    if (requestCount == 15) { // check every 15
        final long currentTime = System.currentTimeMillis();
        final long diff = currentTime - lastRequestTime;
        //if less than a second then sleep for the remainder of the second
        if (diff < 1000) {
            Thread.sleep(1000 - diff);
        }//from  w w  w. j  a  v a  2  s .com
        //reset
        lastRequestTime = System.currentTimeMillis();
        requestCount = 0;
    }

    final URL url = new URL(SERVER + endpoint);
    URLConnection connection = url.openConnection();
    HttpURLConnection httpConnection = (HttpURLConnection) connection;
    httpConnection.setRequestProperty("Content-Type", "application/json");

    final InputStream response = httpConnection.getInputStream();
    int responseCode = httpConnection.getResponseCode();

    if (responseCode != 200) {
        if (responseCode == 429 && httpConnection.getHeaderField("Retry-After") != null) {
            double sleepFloatingPoint = Double.valueOf(httpConnection.getHeaderField("Retry-After"));
            double sleepMillis = 1000 * sleepFloatingPoint;
            Thread.sleep((long) sleepMillis);
            return getContent(endpoint);
        }
        throw new RuntimeException("Response code was not 200. Detected response was " + responseCode);
    }

    String output;
    Reader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(response, "UTF-8"));
        StringBuilder builder = new StringBuilder();
        char[] buffer = new char[8192];
        int read;
        while ((read = reader.read(buffer, 0, buffer.length)) > 0) {
            builder.append(buffer, 0, read);
        }
        output = builder.toString();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException logOrIgnore) {
                logOrIgnore.printStackTrace();
            }
        }
    }

    return output;
}

From source file:och.util.NetUtil.java

public static String invokePost(HttpURLConnection conn, String postBody) throws IOException {

    try {/*from  www  .j  a  v a 2  s .  c om*/

        if (Util.isEmpty(postBody)) {

            conn.setDoOutput(false);

        } else {

            // Send post request
            conn.setDoOutput(true);

            DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
            wr.writeBytes(postBody);
            wr.flush();
            wr.close();
        }

        int code = conn.getResponseCode();
        //It may happen due to keep-alive problem http://stackoverflow.com/questions/1440957/httpurlconnection-getresponsecode-returns-1-on-second-invocation
        if (code == -1) {
            throw new IllegalStateException("NetUtil: conn.getResponseCode() return -1");
        }

        InputStream is = new BufferedInputStream(conn.getInputStream());
        String enc = conn.getHeaderField("Content-Encoding");
        if (enc != null && enc.equalsIgnoreCase("gzip")) {
            is = new GZIPInputStream(is);
        }
        String response = StreamUtil.streamToStr(is);
        return response;
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
}

From source file:gmusic.api.comm.HttpUrlConnector.java

private void setCookie(HttpURLConnection connection) {
    if (!Strings.isNullOrEmpty(connection.getHeaderField("Set-Cookie")) && cookie == null) {
        rawCookie = connection.getHeaderField("Set-Cookie");
        int startIndex = rawCookie.indexOf("xt=") + "xt=".length();
        int endIndex = rawCookie.indexOf(";", startIndex);
        cookie = rawCookie.substring(startIndex, endIndex);
    }/*from   w  ww  .  j a v a  2 s  .c o m*/
}

From source file:com.dream.library.utils.AbFileUtil.java

/**
 * ???xx.??.//from  w  w  w . ja va2  s .co  m
 *
 * @param connection 
 * @return ??
 */
public static String getRealFileName(HttpURLConnection connection) {
    String name = null;
    try {
        if (connection == null) {
            return name;
        }
        if (connection.getResponseCode() == 200) {
            for (int i = 0;; i++) {
                String mime = connection.getHeaderField(i);
                if (mime == null) {
                    break;
                }
                // "Content-Disposition","attachment; filename=1.txt"
                // Content-Length
                if ("content-disposition".equals(connection.getHeaderFieldKey(i).toLowerCase())) {
                    Matcher m = Pattern.compile(".*filename=(.*)").matcher(mime.toLowerCase());
                    if (m.find()) {
                        return m.group(1).replace("\"", "");
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        AbLog.e("???");
    }
    return name;
}

From source file:com.mobile.natal.natalchart.NetworkUtilities.java

/**
 * Sends an HTTP GET request to a url//from w  ww . j a  va  2s.  c  o m
 *
 * @param urlStr            - The URL of the server. (Example: " http://www.yahoo.com/search")
 * @param file              the output file. If it is a folder, it tries to get the file name from the header.
 * @param requestParameters - all the request parameters (Example: "param1=val1&param2=val2").
 *                          Note: This method will add the question mark (?) to the request -
 *                          DO NOT add it yourself
 * @param user              user.
 * @param password          password.
 * @return the file written.
 * @throws Exception if something goes wrong.
 */
public static File sendGetRequest4File(String urlStr, File file, String requestParameters, String user,
        String password) throws Exception {
    if (requestParameters != null && requestParameters.length() > 0) {
        urlStr += "?" + requestParameters;
    }
    HttpURLConnection conn = makeNewConnection(urlStr);
    conn.setRequestMethod("GET");
    // conn.setDoOutput(true);
    conn.setDoInput(true);
    // conn.setChunkedStreamingMode(0);
    conn.setUseCaches(false);

    if (user != null && password != null && user.trim().length() > 0 && password.trim().length() > 0) {
        conn.setRequestProperty("Authorization", getB64Auth(user, password));
    }
    conn.connect();

    if (file.isDirectory()) {
        // try to get the header
        String headerField = conn.getHeaderField("Content-Disposition");
        String fileName = null;
        if (headerField != null) {
            String[] split = headerField.split(";");
            for (String string : split) {
                String pattern = "filename=";
                if (string.toLowerCase().startsWith(pattern)) {
                    fileName = string.replaceFirst(pattern, "");
                    break;
                }
            }
        }
        if (fileName == null) {
            // give a name
            fileName = "FILE_" + TimeUtilities.INSTANCE.TIMESTAMPFORMATTER_LOCAL.format(new Date());
        }
        file = new File(file, fileName);
    }

    InputStream in = null;
    FileOutputStream out = null;
    try {
        in = conn.getInputStream();
        out = new FileOutputStream(file);

        byte[] buffer = new byte[(int) maxBufferSize];
        int bytesRead = in.read(buffer, 0, (int) maxBufferSize);
        while (bytesRead > 0) {
            out.write(buffer, 0, bytesRead);
            bytesRead = in.read(buffer, 0, (int) maxBufferSize);
        }
        out.flush();
    } finally {
        if (in != null)
            in.close();
        if (out != null)
            out.close();
        if (conn != null)
            conn.disconnect();
    }
    return file;
}