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:com.sun.faces.systest.ant.SystestClient.java

/**
 * Execute the test via use of an HttpURLConnection.
 *
 * @throws BuildException if an exception occurs
 *//*from   w  ww  .j av  a2s  .c  om*/
protected void executeHttp() throws BuildException {

    // Construct a summary of the request we will be sending
    String summary = "[" + method + " " + request + "]";
    boolean success = true;
    String result = null;
    Throwable throwable = null;
    HttpURLConnection conn = null;

    try {

        // Configure an HttpURLConnection for this request
        if (log.isDebugEnabled()) {
            log.debug("Configuring HttpURLConnection for this request");
        }
        URL url = new URL("http", host, port, request);
        conn = (HttpURLConnection) url.openConnection();
        conn.setAllowUserInteraction(false);
        conn.setDoInput(true);
        if (inContent != null) {
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Length", "" + inContent.length());
            if (log.isTraceEnabled()) {
                log.trace("INPH: Content-Length: " + inContent.length());
            }
        } else {
            conn.setDoOutput(false);
        }

        // Send the session id cookie (if any)
        if (joinSession && (sessionId != null)) {
            conn.setRequestProperty("Cookie", "JSESSIONID=" + sessionId);
            if (log.isTraceEnabled()) {
                log.trace("INPH: Cookie: JSESSIONID=" + sessionId);
            }
        }

        if (this.redirect && log.isTraceEnabled()) {
            log.trace("FLAG: setInstanceFollowRedirects(" + this.redirect + ")");
        }
        conn.setInstanceFollowRedirects(this.redirect);
        conn.setRequestMethod(method);
        if (inHeaders != null) {
            String headers = inHeaders;
            while (headers.length() > 0) {
                int delimiter = headers.indexOf("##");
                String header = null;
                if (delimiter < 0) {
                    header = headers;
                    headers = "";
                } else {
                    header = headers.substring(0, delimiter);
                    headers = headers.substring(delimiter + 2);
                }
                int colon = header.indexOf(":");
                if (colon < 0)
                    break;
                String name = header.substring(0, colon).trim();
                String value = header.substring(colon + 1).trim();
                conn.setRequestProperty(name, value);
                if (log.isTraceEnabled()) {
                    log.trace("INPH: " + name + ": " + value);
                }
            }
        }

        // Connect to the server and send our output if necessary
        conn.connect();
        if (inContent != null) {
            if (log.isTraceEnabled()) {
                log.trace("INPD: " + inContent);
            }
            OutputStream os = conn.getOutputStream();
            for (int i = 0, length = inContent.length(); i < length; i++)
                os.write(inContent.charAt(i));
            os.close();
        }

        // Acquire the response data, if there is any
        String outData = "";
        String outText = "";
        boolean eol = false;
        InputStream is = conn.getInputStream();
        int lines = 0;
        while (true) {
            String line = read(is);
            if (line == null)
                break;
            if (lines == 0)
                outData = line;
            else
                outText += line + "\r\n";
            saveResponse.add(line);
            lines++;
        }
        is.close();

        // Dump out the response stuff
        if (log.isTraceEnabled()) {
            log.trace("RESP: " + conn.getResponseCode() + " " + conn.getResponseMessage());
        }
        for (int i = 1; i < 1000; i++) {
            String name = conn.getHeaderFieldKey(i);
            String value = conn.getHeaderField(i);
            if ((name == null) || (value == null))
                break;
            if (log.isTraceEnabled()) {
                log.trace("HEAD: " + name + ": " + value);
            }
            save(name, value);
            if ("Set-Cookie".equals(name))
                parseSession(value);
        }
        if (log.isTraceEnabled()) {
            log.trace("DATA: " + outData);
            if (outText.length() > 2) {
                log.trace("TEXT: " + outText);
            }
        }

        // Validate the response against our criteria
        if (success) {
            result = validateStatus(conn.getResponseCode());
            if (result != null)
                success = false;
        }
        if (success) {
            result = validateMessage(conn.getResponseMessage());
            if (result != null)
                success = false;
        }
        if (success) {
            result = validateHeaders();
            if (result != null)
                success = false;
        }
        if (success) {
            result = validateData(outData);
            if (result != null)
                success = false;
        }
        if (success) {
            result = validateGolden();
            if (result != null)
                success = false;
        }

    } catch (Throwable t) {
        if (t instanceof FileNotFoundException) {
            if (status == 404) {
                success = true;
                result = "Not Found";
                throwable = null;
            } else {
                success = false;
                try {
                    result = "Status=" + conn.getResponseCode() + ", Message=" + conn.getResponseMessage();
                } catch (IOException e) {
                    result = e.toString();
                }
                throwable = null;
            }
        } else if (t instanceof ConnectException) {
            success = false;
            result = t.getMessage();
            throwable = null;
        } else {
            success = false;
            result = t.getMessage();
            throwable = t;
        }
    }

    // Log the results of executing this request
    if (success) {
        System.out.println("OK   " + summary);
    } else {
        System.out.println("FAIL " + summary + " " + result);
        if (throwable != null)
            throwable.printStackTrace(System.out);
        if (failonerror) {
            if (throwable != null) {
                throw new BuildException("System test failed", throwable);
            } else {
                throw new BuildException("System test failed");
            }
        }
    }

}

From source file:org.apache.streams.urls.LinkResolver.java

public void unwindLink(String url) {
    Objects.requireNonNull(linkDetails);
    Objects.requireNonNull(url);/*from ww  w .j  a v a  2  s  . co m*/

    // Check url validity
    UrlValidator urlValidator = new UrlValidator();
    if (!urlValidator.isValid(url)) {
        linkDetails.setLinkStatus(LinkDetails.LinkStatus.MALFORMED_URL);
        return;
    }

    // Check to see if they wound up in a redirect loop,
    // IE: 'A' redirects to 'B', then 'B' redirects to 'A'
    if ((linkDetails.getRedirectCount() != null && linkDetails.getRedirectCount() > 0
            && (linkDetails.getOriginalURL().equals(url) || linkDetails.getRedirects().contains(url)))
            || (linkDetails.getRedirectCount() != null
                    && linkDetails.getRedirectCount() > MAX_ALLOWED_REDIRECTS)) {
        linkDetails.setLinkStatus(LinkDetails.LinkStatus.LOOP);
        return;
    }

    if (!linkDetails.getOriginalURL().equals(url))
        linkDetails.getRedirects().add(url);

    HttpURLConnection connection = null;

    // Store where the redirected link will go (if there is one)
    String reDirectedLink = null;

    try {
        // Turn the string into a URL
        URL thisURL = new URL(url);

        // Be sensitive to overloading domains STREAMS-77
        try {
            String host = thisURL.getHost().toLowerCase();
            if (!domainsSensitiveTo.contains(host)) {
                domainsSensitiveTo.add(host);
                long domainWait = LinkResolverHelperFunctions.waitTimeForDomain(thisURL.getHost());
                if (domainWait > 0) {
                    LOGGER.debug("Waiting for domain: {}", domainWait);
                    Thread.sleep(domainWait);
                }
            }
        } catch (Exception e) {
            // noOp
        }

        connection = (HttpURLConnection) new URL(url).openConnection();

        // now we are going to pretend that we are a browser...
        // This is the way my mac works.
        if (!BOTS_ARE_OK.contains(thisURL.getHost())) {
            connection.addRequestProperty("Host", thisURL.getHost());

            // Bots are not 'ok', so we need to spoof the headers
            for (String k : SPOOF_HTTP_HEADERS.keySet())
                connection.addRequestProperty(k, SPOOF_HTTP_HEADERS.get(k));

            // the test to seattlemamadoc.com prompted this change.
            // they auto detect bots by checking the referrer chain and the 'user-agent'
            // this broke the t.co test. t.co URLs are EXPLICITLY ok with bots
            // there is a list for URLS that behave this way at the top in BOTS_ARE_OK
            // smashew 2013-13-2013
            if (linkDetails.getRedirectCount() > 0 && BOTS_ARE_OK.contains(thisURL.getHost()))
                connection.addRequestProperty("Referrer", linkDetails.getOriginalURL());
        }

        connection.setReadTimeout(DEFAULT_HTTP_TIMEOUT);
        connection.setConnectTimeout(DEFAULT_HTTP_TIMEOUT);

        // we want to follow this behavior on our own to ensure that we are getting to the
        // proper place. This is especially true with links that are wounded by special
        // link winders,
        // IE:
        connection.setInstanceFollowRedirects(false);

        if (linkDetails.getCookies() != null)
            for (String cookie : linkDetails.getCookies())
                connection.addRequestProperty("Cookie", cookie.split(";", 1)[0]);

        connection.connect();

        linkDetails.setFinalResponseCode((long) connection.getResponseCode());

        Map<String, List<String>> headers = createCaseInsensitiveMap(connection.getHeaderFields());
        /*
         * If they want us to set cookies, well, then we will set cookies
         * Example URL:
         * http://nyti.ms/1bCpesx
         *****************************************************************/
        if (headers.containsKey(SET_COOKIE_IDENTIFIER))
            linkDetails.getCookies().add(headers.get(SET_COOKIE_IDENTIFIER).get(0));

        switch (linkDetails.getFinalResponseCode().intValue()) {
        /*
         * W3C HTTP Response Codes:
         * http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
         */
        case 200: // HTTP OK
            linkDetails.setFinalURL(connection.getURL().toString());
            linkDetails.setDomain(new URL(linkDetails.getFinalURL()).getHost());
            linkDetails.setLinkStatus(LinkDetails.LinkStatus.SUCCESS);
            break;
        case 300: // Multiple choices
        case 301: // URI has been moved permanently
        case 302: // Found
        case 303: // Primarily for a HTTP Post
        case 304: // Not Modified
        case 306: // This status code is unused but in the redirect block.
        case 307: // Temporary re-direct
            /*
             * Author:
             * Smashew
             *
             * Date: 2013-11-15
             *
             * Note:
             * It is possible that we have already found our final URL. In
             * the event that we have found our final URL, we are going to
             * save this URL as long as it isn't the original URL.
             * We are still going to ask the browser to re-direct, but in the
             * case of yet another redirect, seen with the redbull test
             * this can be followed by a 304, a browser, by W3C standards would
             * still render the page with it's content, but for us to assert
             * a success, we are really hoping for a 304 message.
             *******************************************************************/
            if (!linkDetails.getOriginalURL().toLowerCase()
                    .equals(connection.getURL().toString().toLowerCase()))
                linkDetails.setFinalURL(connection.getURL().toString());
            if (!headers.containsKey(LOCATION_IDENTIFIER)) {
                LOGGER.info("Headers: {}", headers);
                linkDetails.setLinkStatus(LinkDetails.LinkStatus.REDIRECT_ERROR);
            } else {
                linkDetails.setRedirected(Boolean.TRUE);
                linkDetails.setRedirectCount(linkDetails.getRedirectCount() + 1);
                reDirectedLink = connection.getHeaderField(LOCATION_IDENTIFIER);
            }
            break;
        case 305: // User must use the specified proxy (deprecated by W3C)
            break;
        case 401: // Unauthorized (nothing we can do here)
            linkDetails.setLinkStatus(LinkDetails.LinkStatus.UNAUTHORIZED);
            break;
        case 403: // HTTP Forbidden (Nothing we can do here)
            linkDetails.setLinkStatus(LinkDetails.LinkStatus.FORBIDDEN);
            break;
        case 404: // Not Found (Page is not found, nothing we can do with a 404)
            linkDetails.setLinkStatus(LinkDetails.LinkStatus.NOT_FOUND);
            break;
        case 500: // Internal Server Error
        case 501: // Not Implemented
        case 502: // Bad Gateway
        case 503: // Service Unavailable
        case 504: // Gateway Timeout
        case 505: // Version not supported
            linkDetails.setLinkStatus(LinkDetails.LinkStatus.HTTP_ERROR_STATUS);
            break;
        default:
            LOGGER.info("Unrecognized HTTP Response Code: {}", linkDetails.getFinalResponseCode());
            linkDetails.setLinkStatus(LinkDetails.LinkStatus.NOT_FOUND);
            break;
        }
    } catch (MalformedURLException e) {
        // the URL is trash, so, it can't load it.
        linkDetails.setLinkStatus(LinkDetails.LinkStatus.MALFORMED_URL);
    } catch (IOException ex) {
        // there was an issue we are going to set to error.
        linkDetails.setLinkStatus(LinkDetails.LinkStatus.ERROR);
    } catch (Exception ex) {
        // there was an unknown issue we are going to set to exception.
        linkDetails.setLinkStatus(LinkDetails.LinkStatus.EXCEPTION);
    } finally {
        // if the connection is not null, then we need to disconnect to close any underlying resources
        if (connection != null)
            connection.disconnect();
    }

    // If there was a redirection, then we have to keep going
    // Placing this code here should help to satisfy ensuring that the connection object
    // is closed successfully.
    if (reDirectedLink != null)
        unwindLink(reDirectedLink);

}

From source file:org.kymjs.kjframe.http.httpclient.HttpRequestBuilder.java

public HttpResponse execute(HashMap<String, File> files) throws HttpClientException {
    HttpURLConnection conn = null;
    UncloseableInputStream payloadStream = null;
    try {/*from  w ww.  j a  va 2  s.  co  m*/
        //            if (parameters != null && !parameters.isEmpty()) {
        //                final StringBuilder buf = new StringBuilder(256);
        //                if (HTTP_GET.equals(method) || HTTP_HEAD.equals(method)) {
        //                    buf.append('?');
        //                }
        //
        //                int paramIdx = 0;
        //                for (final Map.Entry<String, String> e : parameters.entrySet()) {
        //                    if (paramIdx != 0) {
        //                        buf.append("&");
        //                    }
        //                    final String name = e.getKey();
        //                    final String value = e.getValue();
        //                    buf.append(URLEncoder.encode(name, CONTENT_CHARSET)).append("=")
        //                            .append(URLEncoder.encode(value, CONTENT_CHARSET));
        //                    ++paramIdx;
        //                }
        //
        //                if (!contentSet
        //                        && (HTTP_POST.equals(method) || HTTP_DELETE.equals(method) || HTTP_PUT.equals(method))) {
        //                    try {
        //                        content = buf.toString().getBytes(CONTENT_CHARSET);
        //                    } catch (UnsupportedEncodingException e) {
        //                        // Unlikely to happen.
        //                        throw new HttpClientException("Encoding error", e);
        //                    }
        //                } else {
        //                    uri += buf;
        //                }
        //            }

        conn = (HttpURLConnection) new URL(uri).openConnection();
        conn.setConnectTimeout(hc.getConnectTimeout());
        conn.setReadTimeout(hc.getReadTimeout());
        conn.setAllowUserInteraction(false);
        conn.setInstanceFollowRedirects(false);
        conn.setRequestMethod(method);
        conn.setUseCaches(false);
        conn.setDoInput(true);

        if (headers != null && !headers.isEmpty()) {
            for (final Map.Entry<String, List<String>> e : headers.entrySet()) {
                final List<String> values = e.getValue();
                if (values != null) {
                    final String name = e.getKey();
                    for (final String value : values) {
                        conn.addRequestProperty(name, value);
                    }
                }
            }
        }

        if (cookies != null && !cookies.isEmpty()
                || hc.getInMemoryCookies() != null && !hc.getInMemoryCookies().isEmpty()) {
            final StringBuilder cookieHeaderValue = new StringBuilder(256);
            prepareCookieHeader(cookies, cookieHeaderValue);
            prepareCookieHeader(hc.getInMemoryCookies(), cookieHeaderValue);
            conn.setRequestProperty("Cookie", cookieHeaderValue.toString());
        }

        final String userAgent = hc.getUserAgent();
        if (userAgent != null) {
            conn.setRequestProperty("User-Agent", userAgent);
        }

        conn.setRequestProperty("Connection", "keep-alive");
        conn.setRequestProperty("Accept-Encoding", "gzip,deflate");
        conn.setRequestProperty("Accept-Charset", CONTENT_CHARSET);
        conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY);

        if (conn instanceof HttpsURLConnection) {
            setupSecureConnection(hc.getContext(), (HttpsURLConnection) conn);
        }

        // ?
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : parameters.entrySet()) {
            sb.append(PREFIX);
            sb.append(BOUNDARY);
            sb.append(LINEND);
            sb.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + LINEND);
            sb.append("Content-Type: text/plain; charset=" + CONTENT_CHARSET + LINEND);
            sb.append("Content-Transfer-Encoding: 8bit" + LINEND);
            sb.append(LINEND);
            sb.append(entry.getValue());
            sb.append(LINEND);
        }

        DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
        outStream.write(sb.toString().getBytes());

        // ???
        if (files != null) {
            for (Map.Entry<String, File> file : files.entrySet()) {
                StringBuilder sb1 = new StringBuilder();
                sb1.append(PREFIX);
                sb1.append(BOUNDARY);
                sb1.append(LINEND);
                sb1.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getKey() + "\""
                        + LINEND);
                sb1.append("Content-Type: application/octet-stream; charset=" + CONTENT_CHARSET + LINEND);
                sb1.append(LINEND);
                outStream.write(sb1.toString().getBytes());

                InputStream is = new FileInputStream(file.getValue());
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = is.read(buffer)) != -1) {
                    outStream.write(buffer, 0, len);
                }

                is.close();
                outStream.write(LINEND.getBytes());
            }
        }

        // ?
        byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();
        outStream.write(end_data);
        outStream.flush();
        final int statusCode = conn.getResponseCode();
        if (statusCode == -1) {
            throw new HttpClientException("Invalid response from " + uri);
        }
        if (!expectedStatusCodes.isEmpty() && !expectedStatusCodes.contains(statusCode)) {
            throw new HttpClientException(
                    "Expected status code " + expectedStatusCodes + ", got " + statusCode);
        } else if (expectedStatusCodes.isEmpty() && statusCode / 100 != 2) {
            throw new HttpClientException("Expected status code 2xx, got " + statusCode);
        }

        final Map<String, List<String>> headerFields = conn.getHeaderFields();
        final Map<String, String> inMemoryCookies = hc.getInMemoryCookies();
        if (headerFields != null) {
            final List<String> newCookies = headerFields.get("Set-Cookie");
            if (newCookies != null) {
                for (final String newCookie : newCookies) {
                    final String rawCookie = newCookie.split(";", 2)[0];
                    final int i = rawCookie.indexOf('=');
                    final String name = rawCookie.substring(0, i);
                    final String value = rawCookie.substring(i + 1);
                    inMemoryCookies.put(name, value);
                }
            }
        }

        if (isStatusCodeError(statusCode)) {
            // Got an error: cannot read input.
            payloadStream = new UncloseableInputStream(getErrorStream(conn));
        } else {
            payloadStream = new UncloseableInputStream(getInputStream(conn));
        }
        final HttpResponse resp = new HttpResponse(statusCode, payloadStream,
                headerFields == null ? NO_HEADERS : headerFields, inMemoryCookies);
        if (handler != null) {
            try {
                handler.onResponse(resp);
            } catch (HttpClientException e) {
                throw e;
            } catch (Exception e) {
                throw new HttpClientException("Error in response handler", e);
            }
        } else {
            final File temp = File.createTempFile("httpclient-req-", ".cache", hc.getContext().getCacheDir());
            resp.preload(temp);
            temp.delete();
        }
        return resp;
    } catch (SocketTimeoutException e) {
        if (handler != null) {
            try {
                handler.onTimeout();
                return null;
            } catch (HttpClientException e2) {
                throw e2;
            } catch (Exception e2) {
                throw new HttpClientException("Error in response handler", e2);
            }
        } else {
            throw new HttpClientException("Response timeout from " + uri, e);
        }
    } catch (IOException e) {
        throw new HttpClientException("Connection failed to " + uri, e);
    } finally {
        if (conn != null) {
            if (payloadStream != null) {
                // Fully read Http response:
                // http://docs.oracle.com/javase/6/docs/technotes/guides/net/http-keepalive.html
                try {
                    while (payloadStream.read(buffer) != -1) {
                        ;
                    }
                } catch (IOException ignore) {
                }
                payloadStream.forceClose();
            }
            conn.disconnect();
        }
    }
}

From source file:com.codename1.impl.android.AndroidImplementation.java

public Object connect(String url, boolean read, boolean write, int timeout) throws IOException {
    URL u = new URL(url);
    CookieHandler.setDefault(null);
    URLConnection con = u.openConnection();
    if (con instanceof HttpURLConnection) {
        HttpURLConnection c = (HttpURLConnection) con;
        c.setUseCaches(false);//from   www.  j  a  v a 2s .c  om
        c.setDefaultUseCaches(false);
        c.setInstanceFollowRedirects(false);
        if (timeout > -1) {
            c.setConnectTimeout(timeout);
        }
        if (read) {
            if (timeout > -1) {
                c.setReadTimeout(timeout);
            } else {
                c.setReadTimeout(10000);
            }
        }
        if (android.os.Build.VERSION.SDK_INT > 13) {
            c.setRequestProperty("Connection", "close");
        }
    }
    con.setDoInput(read);
    con.setDoOutput(write);
    return con;
}