Example usage for java.net HttpURLConnection setFollowRedirects

List of usage examples for java.net HttpURLConnection setFollowRedirects

Introduction

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

Prototype

public static void setFollowRedirects(boolean set) 

Source Link

Document

Sets whether HTTP redirects (requests with response code 3xx) should be automatically followed by this class.

Usage

From source file:it.evilsocket.dsploit.core.UpdateService.java

/**
 * is ruby update available?//from   w  ww  .j  ava  2  s. com
 * @return true if ruby can be updated, false otherwise
 */
public static boolean isRubyUpdateAvailable() {
    HttpURLConnection connection = null;
    BufferedReader reader = null;
    String line;
    boolean exitForError = true;
    Double localVersion = System.getLocalRubyVersion();

    try {
        synchronized (mRubyInfo) {
            if (mRubyInfo.version == null) {

                HttpURLConnection.setFollowRedirects(true);
                URL url = new URL(REMOTE_RUBY_VERSION_URL);
                connection = (HttpURLConnection) url.openConnection();
                connection.connect();

                if (connection.getResponseCode() != 200)
                    return false;

                reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                StringBuilder sb = new StringBuilder();

                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }

                JSONObject info = new JSONObject(sb.toString());
                mRubyInfo.url = info.getString("url");
                mRubyInfo.version = info.getDouble("version");
                mRubyInfo.versionString = String.format("%d", mRubyInfo.version.intValue());
                mRubyInfo.path = String.format("%s/%s", System.getStoragePath(), info.getString("name"));
                mRubyInfo.archiver = archiveAlgorithm.valueOf(info.getString("archiver"));
                mRubyInfo.compression = compressionAlgorithm.valueOf(info.getString("compression"));
                mRubyInfo.md5 = info.getString("md5");
                mRubyInfo.sha1 = info.getString("sha1");
                mRubyInfo.outputDir = System.getRubyPath();
            }
            exitForError = false;

            if (Shell.canExecuteInDir(mRubyInfo.outputDir)) {
                mRubyInfo.executableOutputDir = mRubyInfo.outputDir;
            } else {
                String realPath = Shell.getRealPath(mRubyInfo.outputDir);
                if (Shell.canRootExecuteInDir(realPath))
                    mRubyInfo.executableOutputDir = realPath;
                else {
                    Logger.error(String.format("cannot create executable files in '%s' or '%s'",
                            mRubyInfo.outputDir, realPath));
                    return false;
                }
            }

            if (localVersion == null || localVersion < mRubyInfo.version)
                return true;
        }
    } catch (Exception e) {
        System.errorLogging(e);
    } finally {
        try {
            if (reader != null)
                reader.close();
        } catch (Exception e) {
            //ignored
        }
        if (connection != null)
            connection.disconnect();
        if (exitForError)
            mRubyInfo.reset();
    }
    return false;
}

From source file:com.chiorichan.util.WebUtils.java

/**
 * Establishes an HttpURLConnection from a URL, with the correct configuration to receive content from the given URL.
 * /*  www. j ava  2 s . c  o m*/
 * @param url
 *            The URL to set up and receive content from
 * @return A valid HttpURLConnection
 * 
 * @throws IOException
 *             The openConnection() method throws an IOException and the calling method is responsible for handling it.
 */
public static HttpURLConnection openHttpConnection(URL url) throws IOException {
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoInput(true);
    conn.setDoOutput(false);
    System.setProperty("http.agent", getUserAgent());
    conn.setRequestProperty("User-Agent", getUserAgent());
    HttpURLConnection.setFollowRedirects(true);
    conn.setUseCaches(false);
    conn.setInstanceFollowRedirects(true);
    return conn;
}

From source file:com.amazonaws.ipnreturnurlvalidation.SignatureUtilsForOutbound.java

/**
 * Fetches the public key certificate from the given url and caches it in
 * memory./*ww  w  .  j a va 2 s. com*/
 */
private String getPublicKeyCertificateAsString(String certificateUrl) throws SignatureException {
    // 1. Try to fetch from the in-memory cache
    String certificate = keyStore.get(certificateUrl);
    if (certificate != null)
        return certificate;

    // 2. If not found in cache, fetch it
    boolean followRedirects = HttpURLConnection.getFollowRedirects();
    HttpURLConnection.setFollowRedirects(false);
    try {
        certificate = URLReader.getUrlContents(certificateUrl);
    } catch (IOException e) {
        throw new SignatureException(e);
    } finally {
        HttpURLConnection.setFollowRedirects(followRedirects);
    }

    // 3. populate newly fetched certificate in cache.
    keyStore.put(certificateUrl, certificate);

    return certificate;
}

From source file:yandexDisk.YandexDiskAPI.java

public static String getDownloadUrl(String auth, String path) {
    if (auth == null || auth.length() == 0)
        return null;
    try {//from www.j a  v  a  2  s . c o m
        URL Url = new URL(GET_DOWNLOAD_URL + URLEncoder.encode(path, "UTF-8"));
        HttpURLConnection conn = (HttpURLConnection) Url.openConnection();
        if (conn == null)
            return null;
        HttpURLConnection.setFollowRedirects(true);
        conn.setReadTimeout(20000);
        conn.setConnectTimeout(20000);
        conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
        conn.addRequestProperty("Authorization", auth);
        InputStream in = getInputEncoding(conn);
        if (in == null)
            return null;
        String s = getStringFromStream(in);
        if (s == null)
            return null;
        return new JSONObject(s).getString("href");
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:br.bireme.tb.URLS.java

/**
 * Given an url, loads its content (GET - method)
 * @param url url to be loaded// w w w  .j  a v a2s.c o m
 * @return an array with the real location of the page (in case of redirect)
 * and its content.
 * @throws IOException
 */
public static String[] loadPageGet(final URL url) throws IOException {
    if (url == null) {
        throw new NullPointerException("url");
    }
    System.out.print("loading page (GET) : [" + url + "]");
    HttpURLConnection.setFollowRedirects(false);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.setRequestProperty("Accept-Charset", DEFAULT_ENCODING);
    connection.setRequestProperty("User-Agent", "curl/7.29.0");
    connection.setRequestProperty("Accept", "*/*");
    connection.connect();

    int respCode = connection.getResponseCode();
    final StringBuilder builder = new StringBuilder();
    String location = url.toString();

    while ((respCode >= 300) && (respCode <= 399)) {
        location = connection.getHeaderField("Location");
        connection = (HttpURLConnection) new URL(location).openConnection();
        respCode = connection.getResponseCode();
    }

    final boolean respCodeOk = (respCode == 200);
    final BufferedReader reader;
    boolean skipLine = false;

    if (respCodeOk) {
        reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), DEFAULT_ENCODING));
    } else {
        reader = new BufferedReader(new InputStreamReader(connection.getErrorStream(), DEFAULT_ENCODING));
    }

    while (true) {
        String line = reader.readLine();
        if (line == null) {
            break;
        }
        final String line2 = line.trim();

        if (line2.startsWith("<!--")) {
            if (line2.endsWith("-->")) {
                continue;
            }
            skipLine = true;
        } else if (line2.endsWith("-->")) {
            skipLine = false;
            line = "";
        }
        if (!skipLine) {
            builder.append(line);
            builder.append("\n");
        }
    }
    reader.close();
    connection.disconnect();

    if (!respCodeOk) {
        throw new IOException("url=[" + url + "]\ncode=" + respCode + "\n" + builder.toString());
    }
    //System.out.print("+");
    System.out.println(" - OK");

    return new String[] { location, builder.toString() };
}

From source file:com.gelakinetic.mtgfam.FamiliarActivity.java

/**
 * Open an inputStream to the HTML content at the given URL, making recursive calls for
 * redirection (HTTP 301, 302)./*from w w  w  .j a  va2  s.c  om*/
 *
 * @param url            The URL to open a stream to
 * @param logWriter      A PrintWriter to log debug info to. Can be null
 * @param recursionLevel The redirect recursion level. Starts at 0, doesn't go past 10
 * @return An InputStream to the content at the URL, or null
 * @throws IOException Thrown if something goes terribly wrong
 */
private static @Nullable InputStream getHttpInputStream(URL url, @Nullable PrintWriter logWriter,
        int recursionLevel) throws IOException {

    /* Don't allow infinite recursion */
    if (recursionLevel > 10) {
        return null;
    }

    /* Make the URL & connection objects, follow redirects, timeout after 5s */
    HttpURLConnection.setFollowRedirects(true);
    HttpURLConnection connection = (HttpURLConnection) (url).openConnection();
    connection.setConnectTimeout(5000);
    connection.setInstanceFollowRedirects(true);

    /* If the connection is not OK, debug print the response */
    if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
        /* Log the URL and response code */
        if (logWriter != null) {
            logWriter.write("URL : " + url.toString() + '\n');
            logWriter.write("RESP: " + connection.getResponseCode() + '\n');
        }

        /* Comb through header fields for a redirect location */
        URL nextUrl = null;
        for (String key : connection.getHeaderFields().keySet()) {
            /* Log the header */
            if (logWriter != null) {
                logWriter.write("HDR : [" + key + "] " + connection.getHeaderField(key) + '\n');
            }

            /* Found the URL to try next */
            if (key != null && key.equalsIgnoreCase("location")) {
                nextUrl = new URL(connection.getHeaderField(key));
            }
        }

        /* If the next location is still null, comb through the HTML
         * This is kind of a hack for when sites.google.com is serving up malformed 302
         * redirects and all the header fields end up being in this input stream
         */
        if (nextUrl == null) {
            /* Open the stream */
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            int linesRead = 0;
            /* Read one line at a time */
            while ((line = br.readLine()) != null) {
                /* Log the line */
                if (logWriter != null) {
                    logWriter.write("HTML:" + line + '\n');
                }
                /* Check for a location */
                if (line.toLowerCase().contains("location")) {
                    nextUrl = new URL(line.split("\\s+")[1]);
                    break;
                }
                /* Count the line, make sure to quit after 1000 */
                linesRead++;
                if (linesRead > 1000) {
                    break;
                }
            }
        }

        if (nextUrl != null) {
            /* If there is a URL to follow, follow it */
            return getHttpInputStream(nextUrl, logWriter, recursionLevel + 1);
        } else {
            /* Otherwise return null */
            return null;
        }

    } else {
        /* HTTP response is A-OK. Return the inputStream */
        return connection.getInputStream();
    }
}

From source file:com.depas.utils.FileUtils.java

public static boolean fileExistByURL(String urlName) {
    try {//from   ww  w .  j av a  2s.co m
        HttpURLConnection.setFollowRedirects(false);
        // note : you may also need
        //        HttpURLConnection.setInstanceFollowRedirects(false)
        HttpURLConnection con = (HttpURLConnection) new URL(urlName).openConnection();
        con.setRequestMethod("HEAD");
        return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
    } catch (Exception e) {
        logger.warn("Error checking if a file exists by URL [URLName=" + urlName + "]: " + e, e);
        return false;
    }
}

From source file:com.centurylink.mdw.util.HttpHelper.java

public String mkcol() throws IOException {
    if (connection == null) {
        if (proxy == null)
            connection = (HttpURLConnection) url.openConnection();
        else//from  ww  w .ja v a2 s. c  o m
            connection = (HttpURLConnection) url.openConnection(proxy);
    }

    prepareConnection(connection);

    connection.setDoOutput(false);
    connection.setRequestMethod("MKCOL");

    HttpURLConnection.setFollowRedirects(true);

    InputStream is = null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        is = connection.getInputStream();
        byte[] buffer = new byte[2048];
        while (maxBytes == -1 || baos.size() < maxBytes) {
            int bytesRead = is.read(buffer);
            if (bytesRead == -1)
                break;
            baos.write(buffer, 0, bytesRead);
        }
    } finally {
        if (is != null)
            is.close();
        connection.disconnect();
        responseCode = connection.getResponseCode();
        responseMessage = connection.getResponseMessage();
        headers = new HashMap<String, String>();
        for (String headerKey : connection.getHeaderFields().keySet()) {
            headers.put(headerKey, connection.getHeaderField(headerKey));
        }
    }

    return baos.toString();
}

From source file:org.zend.sdklib.internal.target.ZendDevCloud.java

private boolean setFollowRedirect() {
    final boolean orginal = HttpURLConnection.getFollowRedirects();
    HttpURLConnection.setFollowRedirects(false);
    return orginal;
}