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:com.commonsware.android.EMusicDownloader.SingleBook.java

public void sampleButtonPressed(View button) {

    Log.d("EMD - ", "Attempting to play sample");

    if (sampleURL.contains("sample")) {
        final ProgressDialog dialog = ProgressDialog.show(this, "", getString(R.string.getting_sample_loation),
                true, true);//  ww w.  j av a2s.co  m

        Thread t5 = new Thread() {
            public void run() {

                String addresstemp = "";

                try {
                    URL u = new URL(sampleURL);
                    HttpURLConnection c = (HttpURLConnection) u.openConnection();
                    c.setRequestMethod("GET");
                    //c.setDoOutput(true);
                    c.setFollowRedirects(true);
                    c.setInstanceFollowRedirects(true);
                    c.connect();
                    InputStream in = c.getInputStream();

                    InputStreamReader inputreader = new InputStreamReader(in);
                    BufferedReader buffreader = new BufferedReader(inputreader);

                    String line;

                    // read every line of the file into the line-variable, on line at the time
                    while ((line = buffreader.readLine()).length() > 0) {
                        //Log.d("EMD - ","Read line"+line);
                        if (line.contains(".mp3") || line.contains("samples.emusic")
                                || line.contains("samples.nl.emusic")) {
                            addresstemp = line;
                            //Log.d("EMD - ","Found MP3 Address "+addresstemp);
                            mp3Address = addresstemp;
                            if (!vKilled && dialog.isShowing()) {
                                handlerPlay.sendEmptyMessage(0);
                            }
                            if (dialog.isShowing()) {
                                dialog.dismiss();
                            }
                        }
                    }

                    in.close();
                    c.disconnect();
                    if (dialog.isShowing()) {
                        dialog.dismiss();
                    }
                } catch (Exception ef) {
                    Log.d("EMD - ", "Getting mp3 address failed ");
                    if (dialog.isShowing()) {
                        dialog.dismiss();
                    }
                }
            }
        };
        t5.start();
    } else {
        Toast.makeText(thisActivity, R.string.no_sample_available, Toast.LENGTH_SHORT).show();
    }
}

From source file:org.asqatasun.websnapshot.urlmanager.utils.UrlUtils.java

/**
 *
 * @param targetUrl// ww  w  . ja v  a2s.  c o  m
 * @return
 */
public static String checkURLAvailable(String targetUrl) {
    try {
        URL url = new URL(targetUrl);
        URLConnection urlConnection = url.openConnection();

        HttpURLConnection.setFollowRedirects(true);
        HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
        httpURLConnection.setRequestMethod("HEAD");

        if (httpURLConnection.getResponseCode() >= MIN_HTTP_VALID_CODE
                && httpURLConnection.getResponseCode() < MAX_HTTP_VALID_CODE) {
            return URL_AVAILABLE;
        }
        urlConnection = url.openConnection();
        httpURLConnection.disconnect();
        HttpURLConnection.setFollowRedirects(true);
        httpURLConnection = (HttpURLConnection) urlConnection;
        httpURLConnection.setRequestMethod("GET");

        if (httpURLConnection.getResponseCode() >= MIN_HTTP_VALID_CODE
                && httpURLConnection.getResponseCode() < MAX_HTTP_VALID_CODE) {
            return URL_AVAILABLE;
        } else {
            return Integer.toString(httpURLConnection.getResponseCode()) + " "
                    + httpURLConnection.getResponseMessage();
        }
    } catch (Exception e) {
        return URL_NOT_AVAILABLE;
    }
}

From source file:net.technicpack.launchercore.util.DownloadUtils.java

public static String getETag(String address) {
    String md5 = "";

    try {// ww  w.j a v  a2s . c  o m
        URL url = new URL(address);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(false);
        System.setProperty("http.agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19");
        conn.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19");
        HttpURLConnection.setFollowRedirects(true);
        conn.setUseCaches(false);
        conn.setInstanceFollowRedirects(true);

        String eTag = conn.getHeaderField("ETag");
        if (eTag != null) {
            eTag = eTag.replaceAll("^\"|\"$", "");
            if (eTag.length() == 32) {
                md5 = eTag;
            }
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return md5;
}

From source file:com.sun.faces.systest.ant.SystestClient.java

/**
 * Read and save the contents of the golden file for this test, if any.
 * Otherwise, the <code>saveGolden</code> list will be empty.
 *
 * @throws IOException if an input/output error occurs
 *//*from   ww w .java2 s . c  o m*/
protected void readGolden() throws IOException {

    // Was a golden file specified?
    saveGolden.clear();
    if (golden == null) {
        return;
    }

    // Create a connection to receive the golden file contents
    URL url = new URL("http", host, port, golden);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setAllowUserInteraction(false);
    conn.setDoInput(true);
    conn.setDoOutput(false);
    conn.setFollowRedirects(true);
    conn.setRequestMethod("GET");

    // Connect to the server and retrieve the golden file
    conn.connect();
    InputStream is = conn.getInputStream();
    while (true) {
        String line = read(is);
        if (line == null) {
            break;
        }
        saveGolden.add(line);
    }
    is.close();
    conn.disconnect();

}

From source file:com.sun.faces.systest.ant.SystestClient.java

/**
 * Read and save the contents of the ignore file for this test, if any.
 * Otherwise, the <code>saveIgnore</code> list will be empty.
 *
 * @throws IOException if an input/output error occurs
 *//*  ww w  .j  a v a2s .c  o  m*/
protected void readIgnore() throws IOException {

    // Was an ignore file specified?
    saveIgnore.clear();
    if (ignore == null) {
        return;
    }

    // Create a connection to receive the ignore file contents
    URL url = new URL("http", host, port, ignore);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setAllowUserInteraction(false);
    conn.setDoInput(true);
    conn.setDoOutput(false);
    conn.setFollowRedirects(true);
    conn.setRequestMethod("GET");

    // Connect to the server and retrieve the ignore file
    conn.connect();
    InputStream is = conn.getInputStream();
    while (true) {
        String line = read(is);
        if (line == null) {
            break;
        }
        saveIgnore.add(line);
    }
    is.close();
    conn.disconnect();

}

From source file:com.pkrete.locationservice.admin.util.WebUtil.java

/**
 * Checks if the given URL exists./*ww  w .j a  v  a2 s.  com*/
 *
 * @param url the URL to be checked
 * @return if the URL is exists returns true, otherwise returns false
 */
public static boolean exists(String url) {
    try {
        HttpURLConnection.setFollowRedirects(true);
        HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
        con.setRequestMethod("HEAD");
        // HTTP statuses 200 and 302 are accepted
        return (con.getResponseCode() == HttpURLConnection.HTTP_OK
                || con.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        return false;
    }
}

From source file:it.evilsocket.dsploit.net.GitHubParser.java

private void fetchBranches() throws IOException, JSONException {
    HttpURLConnection connection;
    HttpURLConnection.setFollowRedirects(true);
    URL url = new URL(String.format(BRANCHES_URL, mUsername, mProject));
    connection = (HttpURLConnection) url.openConnection();
    connection.connect();/*  www.  j  av  a2s.c o  m*/
    int ret = connection.getResponseCode();

    if (ret != 200)
        throw new IOException(String.format("unable to retrieve branches from github: '%s' => %d",
                String.format(BRANCHES_URL, mUsername, mProject), ret));

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

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

    mBranches = new JSONArray(sb.toString());
}

From source file:it.evilsocket.dsploit.net.GemParser.java

private void fetch() throws JSONException, IOException {
    HttpURLConnection connection = null;
    BufferedReader reader = null;

    try {/* w w w  .  j  ava2  s  .c o m*/
        HttpURLConnection.setFollowRedirects(true);
        connection = (HttpURLConnection) (new URL(mUrl)).openConnection();
        connection.connect();
        int ret = connection.getResponseCode();
        if (ret != 200)
            throw new IOException("cannot retrieve remote json: " + ret);
        reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line;

        while ((line = reader.readLine()) != null)
            sb.append(line);
        JSONArray jGems = new JSONArray(sb.toString());
        mGems = new RemoteGemInfo[jGems.length()];
        for (int i = 0; i < jGems.length(); i++) {
            JSONObject gem = jGems.getJSONObject(i);
            mGems[i] = new RemoteGemInfo(gem.getString("name"), gem.getString("version"),
                    (new DateTime(gem.getString("uploaded"))).toDate());
        }
    } finally {
        if (reader != null)
            reader.close();
        if (connection != null)
            connection.disconnect();
    }
}

From source file:net.technicpack.launchercore.util.Utils.java

/**
 * Establishes an HttpURLConnection from a URL, with the correct configuration to receive content from the given URL.
 *
 * @param url The URL to set up and receive content from
 * @return A valid HttpURLConnection/*from   ww  w . j  av a  2s. c  o  m*/
 *
 * @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",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19");
    conn.setRequestProperty("User-Agent",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19");
    HttpURLConnection.setFollowRedirects(true);
    conn.setUseCaches(false);
    conn.setInstanceFollowRedirects(true);
    return conn;
}

From source file:fi.hip.sicx.store.HIPStoreClient.java

private HttpURLConnection getConnection(String path) throws Exception {

    LocalProperties props = LocalProperties.getInstance();
    URL url = new URL(props.getProperty("hipstore.url", "https://localhost:7443/remotestore") + path);
    HttpURLConnection uc = (HttpURLConnection) url.openConnection();
    HttpURLConnection.setFollowRedirects(true);

    if (uc instanceof HttpsURLConnection) {
        HttpsURLConnection conn = (HttpsURLConnection) uc;
        conn.setSSLSocketFactory(wrapper.getSocketFactory());
    }//from   w ww  . j a  v  a 2s  .  c  o  m
    //conn.setRequestProperty("Connection","Keep-Alive");
    return uc;
}