Example usage for java.net HttpURLConnection HTTP_MOVED_TEMP

List of usage examples for java.net HttpURLConnection HTTP_MOVED_TEMP

Introduction

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

Prototype

int HTTP_MOVED_TEMP

To view the source code for java.net HttpURLConnection HTTP_MOVED_TEMP.

Click Source Link

Document

HTTP Status-Code 302: Temporary Redirect.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    HttpURLConnection.setFollowRedirects(false);
    HttpURLConnection con = (HttpURLConnection) new URL("http://www.google.coom").openConnection();
    con.setRequestMethod("HEAD");
    System.out.println(con.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP);
}

From source file:Main.java

private static String getRedrectUrl(String url) throws IOException {
    Response response = Jsoup.connect(url).followRedirects(false).execute();
    int status = response.statusCode();
    if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM
            || status == HttpURLConnection.HTTP_SEE_OTHER) {
        return response.header("location");
    }//w ww  .  ja  va2 s.  c o m
    return null;
}

From source file:Main.java

public static URI unredirect(URI uri) throws IOException {
    if (!REDIRECTOR_DOMAINS.contains(uri.getHost())) {
        return uri;
    }// w w w  . ja  v  a  2 s. co m
    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:com.shopgun.android.sdk.network.impl.DefaultRedirectProtocol.java

@Override
public boolean isRedirectRequested(Request<?> request, HttpResponse response, ArrayList<URL> previouslyVisited)
        throws IOException {
    int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode == HttpURLConnection.HTTP_MOVED_PERM || statusCode == HttpURLConnection.HTTP_MOVED_TEMP
            || statusCode == HttpURLConnection.HTTP_SEE_OTHER) {
        request.addEvent("request-redirected-statuscode-" + statusCode);
        return true;
    }/*  w ww .  j a  v a  2  s .  co m*/
    return false;
}

From source file:org.jboss.as.test.integration.management.console.WebConsoleRedirectionTestCase.java

@Test
public void testRedirectionInAdminMode() throws Exception {
    ServerReload.executeReloadAndWaitForCompletion(managementClient.getControllerClient(), true);
    try {/*  ww w.  j  a  v  a 2 s  . c om*/
        final HttpURLConnection connection = getConnection();
        assertEquals(HttpURLConnection.HTTP_MOVED_TEMP, connection.getResponseCode());
        String location = connection.getHeaderFields().get(Headers.LOCATION_STRING).get(0);
        assertEquals("/consoleerror/noConsoleForAdminModeError.html", location);
    } finally {
        ServerReload.executeReloadAndWaitForCompletion(managementClient.getControllerClient(), false);
    }
}

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

/**
 * Checks if the given URL exists./*from   w w w.  j  a  va2 s. c  om*/
 *
 * @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:com.ratebeer.android.api.command.SendBeerReplyCommand.java

@Override
protected void makeRequest(ApiConnection apiConnection) throws ApiException {
    ApiConnection.ensureLogin(apiConnection, getUserSettings());
    apiConnection.post("http://www.ratebeer.com/SaveReply.asp",
            Arrays.asList(new BasicNameValuePair("MessID", Integer.toString(replyTo)),
                    new BasicNameValuePair("Referrer", "/showmessage_new.asp?messageID=" + replyTo),
                    new BasicNameValuePair("UserID", Integer.toString(recipient)),
                    new BasicNameValuePair("Body", body)),
            HttpURLConnection.HTTP_MOVED_TEMP);
}

From source file:com.ratebeer.android.api.command.SendBeerMailCommand.java

@Override
protected void makeRequest(ApiConnection apiConnection) throws ApiException {
    ApiConnection.ensureLogin(apiConnection, getUserSettings());
    apiConnection.post("http://www.ratebeer.com/savemessage/",
            Arrays.asList(new BasicNameValuePair("nSource", Integer.toString(getUserSettings().getUserID())),
                    new BasicNameValuePair("Referrer", "http://www.ratebeer.com/user/messages/0/"),
                    new BasicNameValuePair("UserName", "0"), new BasicNameValuePair("RecipientName", sendTo),
                    new BasicNameValuePair("Subject", subject), new BasicNameValuePair("Body", body)),
            HttpURLConnection.HTTP_MOVED_TEMP);
}

From source file:Main.java

/**
 * Return an {@link InputStream} from the given url or null if failed to retrieve the content
 * //from   ww w  .j  a va 2s . com
 * @param uri
 * @return
 */
static InputStream openRemoteInputStream(Uri uri) {
    java.net.URL finalUrl;
    try {
        finalUrl = new java.net.URL(uri.toString());
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return null;
    }

    HttpURLConnection connection;
    try {
        connection = (HttpURLConnection) finalUrl.openConnection();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

    connection.setInstanceFollowRedirects(false);
    int code;
    try {
        code = connection.getResponseCode();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

    // permanent redirection
    if (code == HttpURLConnection.HTTP_MOVED_PERM || code == HttpURLConnection.HTTP_MOVED_TEMP
            || code == HttpURLConnection.HTTP_SEE_OTHER) {
        String newLocation = connection.getHeaderField("Location");
        return openRemoteInputStream(Uri.parse(newLocation));
    }

    try {
        return (InputStream) finalUrl.getContent();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:org.jboss.as.test.integration.management.console.WebConsoleRedirectionTestCase.java

@Test
public void testRedirectionInNormalMode() throws Exception {
    final HttpURLConnection connection = getConnection();
    assertEquals(HttpURLConnection.HTTP_MOVED_TEMP, connection.getResponseCode());
    String location = connection.getHeaderFields().get(Headers.LOCATION_STRING).get(0);
    assertEquals("/console/index.html", location);
}