Example usage for java.net HttpURLConnection setInstanceFollowRedirects

List of usage examples for java.net HttpURLConnection setInstanceFollowRedirects

Introduction

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

Prototype

public void setInstanceFollowRedirects(boolean followRedirects) 

Source Link

Document

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

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    HttpURLConnection.setFollowRedirects(false);

    URL url = new URL("http://hostname:80");
    URLConnection conn = url.openConnection();

    HttpURLConnection httpConn = (HttpURLConnection) conn;
    httpConn.setInstanceFollowRedirects(false);

    conn.connect();/* w  w w . j  av a  2s.  c o m*/
}

From source file:GoogleImages.java

public static void main(String[] args) throws InterruptedException {
    String searchTerm = "s woman"; // term to search for (use spaces to separate terms)
    int offset = 40; // we can only 20 results at a time - use this to offset and get more!
    String fileSize = "50mp"; // specify file size in mexapixels (S/M/L not figured out yet)
    String source = null; // string to save raw HTML source code

    // format spaces in URL to avoid problems
    searchTerm = searchTerm.replaceAll(" ", "%20");

    // get Google image search HTML source code; mostly built from PhyloWidget example:
    // http://code.google.com/p/phylowidget/source/browse/trunk/PhyloWidget/src/org/phylowidget/render/images/ImageSearcher.java
    int offset2 = 0;
    Set urlsss = new HashSet<String>();
    while (offset2 < 600) {
        try {//w w w. ja v a  2  s . co m
            URL query = new URL("https://www.google.ru/search?start=" + offset2
                    + "&q=angry+woman&newwindow=1&client=opera&hs=bPE&source=lnms&tbm=isch&sa=X&ved=0ahUKEwiAgcKozIfNAhWoHJoKHSb_AUoQ_AUIBygB&biw=1517&bih=731&dpr=0.9#imgrc=G_1tH3YOPcc8KM%3A");
            HttpURLConnection urlc = (HttpURLConnection) query.openConnection(); // start connection...
            urlc.setInstanceFollowRedirects(true);
            urlc.setRequestProperty("User-Agent", "");
            urlc.connect();
            BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream())); // stream in HTTP source to file
            StringBuffer response = new StringBuffer();
            char[] buffer = new char[1024];
            while (true) {
                int charsRead = in.read(buffer);
                if (charsRead == -1) {
                    break;
                }
                response.append(buffer, 0, charsRead);
            }
            in.close(); // close input stream (also closes network connection)
            source = response.toString();
        } // any problems connecting? let us know
        catch (Exception e) {
            e.printStackTrace();
        }

        // print full source code (for debugging)
        // println(source);
        // extract image URLs only, starting with 'imgurl'
        if (source != null) {
            //                System.out.println(source);
            int c = StringUtils.countMatches(source, "http://www.vmir.su");
            System.out.println(c);
            int index = source.indexOf("src=");
            System.out.println(source.subSequence(index, index + 200));
            while (index >= 0) {
                System.out.println(index);
                index = source.indexOf("src=", index + 1);
                if (index == -1) {
                    break;
                }
                String rr = source.substring(index,
                        index + 200 > source.length() ? source.length() : index + 200);

                if (rr.contains("\"")) {
                    rr = rr.substring(5, rr.indexOf("\"", 5));
                }
                System.out.println(rr);
                urlsss.add(rr);
            }
        }
        offset2 += 20;
        Thread.sleep(1000);
        System.out.println("off set = " + offset2);

    }

    System.out.println(urlsss);
    urlsss.forEach(new Consumer<String>() {

        public void accept(String s) {
            try {
                saveImage(s, "C:\\Users\\Java\\Desktop\\ang\\" + UUID.randomUUID().toString() + ".jpg");
            } catch (IOException ex) {
                Logger.getLogger(GoogleImages.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });
    //            String[][] m = matchAll(source, "img height=\"\\d+\" src=\"([^\"]+)\"");

    // older regex, no longer working but left for posterity
    // built partially from: http://www.mkyong.com/regular-expressions/how-to-validate-image-file-extension-with-regular-expression
    // String[][] m = matchAll(source, "imgurl=(.*?\\.(?i)(jpg|jpeg|png|gif|bmp|tif|tiff))");    // (?i) means case-insensitive
    //            for (int i = 0; i < m.length; i++) {                                                          // iterate all results of the match
    //                println(i + ":\t" + m[i][1]);                                                         // print (or store them)**
    //            }
}

From source file:org.apache.oltu.oauth2.client.OAuthClientTest.java

public static void main(String[] args) throws OAuthSystemException, IOException {
    String callback = "http://localhost:8080/";
    String clientId = "131804060198305";
    String secret = "3acb294b071c9aec86d60ae3daf32a93";

    String host = "http://smoke-track.herokuapp.com";
    host = "http://localhost:3000";
    String authUri = host + "/oauth/authorize";
    String tokenUri = host + "/oauth/token";
    String appUri = host + "/habits";

    callback = "http://localhost:8080";

    clientId = "728ad798943fff1afd90e79765e9534ef52a5b166cfd25f055d1c8ff6f3ae7fd";
    secret = "3728e0449052b616e2465c04d3cbd792f2d37e70ca64075708bfe8b53c28d529";

    clientId = "e42ae40e269d9a546316f93e42edf52a18934c6a68de035f8b615343c4b81eb0";
    secret = "3b1a720c311321c29852dc4735517f6ece0c991d4be677539cb430c7ee4d1097";

    try {// w w  w  .j  av a  2s .c o m
        OAuthClientRequest request = OAuthClientRequest.authorizationLocation(authUri).setClientId(clientId)
                .setRedirectURI(callback).setResponseType("code").buildQueryMessage();

        //in web application you make redirection to uri:
        System.out.println("Visit: " + request.getLocationUri() + "\nand grant permission");

        System.out.print("Now enter the OAuth code you have received in redirect uri ");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String code = br.readLine();

        request = OAuthClientRequest.tokenLocation(tokenUri).setGrantType(GrantType.AUTHORIZATION_CODE)
                .setClientId(clientId).setClientSecret(secret).setRedirectURI(callback).setCode(code)
                .buildBodyMessage();

        OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());

        OAuthJSONAccessTokenResponse oAuthResponse = oAuthClient.accessToken(request,
                OAuthJSONAccessTokenResponse.class);

        System.out.println("Access Token: " + oAuthResponse.getAccessToken() + ", Expires in: "
                + oAuthResponse.getExpiresIn());

        URL appURL = new URL(appUri);
        HttpURLConnection con = (HttpURLConnection) appURL.openConnection();
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/json");
        con.setRequestProperty("Accept", "application/json");
        con.setRequestProperty("Accept-Encoding", "gzip, deflate");
        con.setRequestProperty("Authorization", "Bearer " + oAuthResponse.getAccessToken());

        JSONObject body = new JSONObject();
        body.put("color", "green");
        body.put("name", "Java Test");
        body.put("description", "Test Habit");

        byte[] bodyBytes = body.toString().getBytes();

        con.setRequestProperty("Content-Length", Integer.toString(bodyBytes.length));

        con.setInstanceFollowRedirects(false);
        con.setUseCaches(false);
        con.setDoInput(true);
        con.setDoOutput(true);

        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.write(bodyBytes);
        wr.flush();
        wr.close();

        //InputStream is = con.getInputStream();
        int status = con.getResponseCode();
        System.out.println("Status: " + status);
    } catch (OAuthProblemException e) {
        System.out.println("OAuth error: " + e.getError());
        System.out.println("OAuth error description: " + e.getDescription());
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String jsonGetRequest(String urlQueryString) {
    String json = null;/*from  w  ww .  j  a v  a2s .c o m*/
    try {
        URL url = new URL(urlQueryString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setInstanceFollowRedirects(false);
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("charset", "utf-8");
        connection.connect();
        InputStream inStream = connection.getInputStream();
        json = streamToString(inStream); // input stream to string
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return json;
}

From source file:Main.java

/**
 * Return '' or error message if error occurs during URL connection.
 * /*from   w  w w . jav  a2s  .  co  m*/
 * @param url   The URL to ckeck
 * @return
 */
public static String getUrlStatus(String url) {
    URL u;
    URLConnection conn;
    int connectionTimeout = 500;
    try {
        u = new URL(url);
        conn = u.openConnection();
        conn.setConnectTimeout(connectionTimeout);

        // TODO : set proxy

        if (conn instanceof HttpURLConnection) {
            HttpURLConnection httpConnection = (HttpURLConnection) conn;
            httpConnection.setInstanceFollowRedirects(true);
            httpConnection.connect();
            httpConnection.disconnect();
            // FIXME : some URL return HTTP200 with an empty reply from server 
            // which trigger SocketException unexpected end of file from server
            int code = httpConnection.getResponseCode();

            if (code == HttpURLConnection.HTTP_OK) {
                return "";
            } else {
                return "Status: " + code;
            }
        } // TODO : Other type of URLConnection
    } catch (Exception e) {
        e.printStackTrace();
        return e.toString();
    }

    return "";
}

From source file:Main.java

public static URI unredirect(URI uri) throws IOException {
    if (!REDIRECTOR_DOMAINS.contains(uri.getHost())) {
        return uri;
    }/*from w w w  .ja  va  2 s  .  c  om*/
    URL url = uri.toURL();

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setInstanceFollowRedirects(false);
    connection.setDoInput(false);
    connection.setRequestMethod("HEAD");
    connection.setRequestProperty("User-Agent", "ZXing (Android)");
    try {
        connection.connect();
        switch (connection.getResponseCode()) {
        case HttpURLConnection.HTTP_MULT_CHOICE:
        case HttpURLConnection.HTTP_MOVED_PERM:
        case HttpURLConnection.HTTP_MOVED_TEMP:
        case HttpURLConnection.HTTP_SEE_OTHER:
        case 307: // No constant for 307 Temporary Redirect ?
            String location = connection.getHeaderField("Location");
            if (location != null) {
                try {
                    return new URI(location);
                } catch (URISyntaxException e) {
                    // nevermind
                }
            }
        }
        return uri;
    } finally {
        connection.disconnect();
    }
}

From source file:Main.java

public static String executePost(String url, String parameters) throws IOException {

    URL request = new URL(url);
    HttpURLConnection connection = (HttpURLConnection) request.openConnection();
    connection.setDoOutput(true);//from w  w  w.j  a  v a2 s .c o  m
    connection.setDoInput(true);
    connection.setInstanceFollowRedirects(false);
    connection.setRequestMethod("POST");
    connection.setRequestProperty("User-Agent", "StripeConnectAndroid");
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    connection.setRequestProperty("charset", "utf-8");
    connection.setRequestProperty("Content-Length", "" + Integer.toString(parameters.getBytes().length));
    connection.setUseCaches(false);

    DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
    wr.writeBytes(parameters);
    wr.flush();
    wr.close();

    String response = streamToString(connection.getInputStream());
    connection.disconnect();
    return response;

}

From source file:Main.java

/**
 * On some devices we have to hack:/*from  www .j  a v  a  2s . c om*/
 * http://developers.sun.com/mobility/reference/techart/design_guidelines/http_redirection.html
 * @return the resolved url if any. Or null if it couldn't resolve the url
 * (within the specified time) or the same url if response code is OK
 */
public static String getResolvedUrl(String urlAsString, int timeout) {
    try {
        URL url = new URL(urlAsString);
        //using proxy may increase latency
        HttpURLConnection hConn = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
        // force no follow

        hConn.setInstanceFollowRedirects(false);
        // the program doesn't care what the content actually is !!
        // http://java.sun.com/developer/JDCTechTips/2003/tt0422.html
        hConn.setRequestMethod("HEAD");
        // default is 0 => infinity waiting
        hConn.setConnectTimeout(timeout);
        hConn.setReadTimeout(timeout);
        hConn.connect();
        int responseCode = hConn.getResponseCode();
        hConn.getInputStream().close();
        if (responseCode == HttpURLConnection.HTTP_OK)
            return urlAsString;

        String loc = hConn.getHeaderField("Location");
        if (responseCode == HttpURLConnection.HTTP_MOVED_PERM && loc != null)
            return loc.replaceAll(" ", "+");

    } catch (Exception ex) {
    }
    return "";
}

From source file:org.jenkinsci.plugins.mber.FileDownloadCallable.java

public static String followRedirects(final String url) throws IOException {
    // Turn off auto redirect follow so we can read the final URL ourselves.
    // The internal parsing doesn't work with some of the headers used by Amazon.
    final HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
    connection.setUseCaches(false);/*  w  w  w . j  av a 2 s. c om*/
    connection.setInstanceFollowRedirects(false);
    connection.connect();

    // Pull the redirect URL out of the "Location" header. Follow it recursively since it might be chained.
    if (connection.getResponseCode() == HttpURLConnection.HTTP_MOVED_PERM
            || connection.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP) {
        final String redirectURL = connection.getHeaderField("Location");
        return followRedirects(redirectURL);
    }

    return url;
}

From source file:Main.java

public static String stringFromHttpPost(String urlStr, String body) {
    HttpURLConnection conn;
    try {/*from   ww w.  ja  va  2s.co m*/
        URL e = new URL(urlStr);
        conn = (HttpURLConnection) e.openConnection();
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setInstanceFollowRedirects(true);
        conn.setRequestMethod("POST");
        OutputStream os1 = conn.getOutputStream();
        DataOutputStream out1 = new DataOutputStream(os1);
        out1.write(body.getBytes("UTF-8"));
        out1.flush();
        conn.connect();
        String line;
        BufferedReader reader;
        StringBuffer sb = new StringBuffer();
        if ("gzip".equals(conn.getHeaderField("Content-Encoding"))) {
            reader = new BufferedReader(
                    new InputStreamReader(new GZIPInputStream(conn.getInputStream()), "UTF-8"));
        } else {
            reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
        }
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        return sb.toString();
    } catch (Exception e) {
        e.printStackTrace();
        logError(e.getMessage());
    }
    return null;
}