Example usage for java.net HttpURLConnection HTTP_SEE_OTHER

List of usage examples for java.net HttpURLConnection HTTP_SEE_OTHER

Introduction

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

Prototype

int HTTP_SEE_OTHER

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

Click Source Link

Document

HTTP Status-Code 303: See Other.

Usage

From source file:com.techventus.server.voice.Voice.java

/**
 * HTTP GET request for a given URL String.
 * /*from   w  ww  .j  a  v  a2s .c  o m*/
 * @param urlString
 *            the url string
 * @return the string
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
String get(String urlString) throws IOException {
    URL url = new URL(urlString);
    //+ "?auth=" + URLEncoder.encode(authToken, enc));

    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestProperty("Authorization", "GoogleLogin auth=" + authToken);
    conn.setRequestProperty("User-agent", USER_AGENT);
    conn.setInstanceFollowRedirects(false); // will follow redirects of same protocol http to http, but does not follow from http to https for example if set to true

    // Get the response
    conn.connect();
    int responseCode = conn.getResponseCode();
    if (PRINT_TO_CONSOLE)
        System.out.println(urlString + " - " + conn.getResponseMessage());
    InputStream is;
    if (responseCode == 200) {
        is = conn.getInputStream();
    } else if (responseCode == HttpURLConnection.HTTP_MOVED_PERM
            || responseCode == HttpURLConnection.HTTP_MOVED_TEMP
            || responseCode == HttpURLConnection.HTTP_SEE_OTHER || responseCode == 307) {
        redirectCounter++;
        if (redirectCounter > MAX_REDIRECTS) {
            redirectCounter = 0;
            throw new IOException(urlString + " : " + conn.getResponseMessage() + "(" + responseCode
                    + ") : Too manny redirects. exiting.");
        }
        String location = conn.getHeaderField("Location");
        if (location != null && !location.equals("")) {
            System.out.println(urlString + " - " + responseCode + " - new URL: " + location);
            return get(location);
        } else {
            throw new IOException(urlString + " : " + conn.getResponseMessage() + "(" + responseCode
                    + ") : Received moved answer but no Location. exiting.");
        }
    } else {
        is = conn.getErrorStream();
    }
    redirectCounter = 0;

    if (is == null) {
        throw new IOException(urlString + " : " + conn.getResponseMessage() + "(" + responseCode
                + ") : InputStream was null : exiting.");
    }

    String result = "";
    try {
        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));

        StringBuffer sb = new StringBuffer();
        String line;
        while ((line = rd.readLine()) != null) {
            sb.append(line + "\n\r");
        }
        rd.close();
        result = sb.toString();
    } catch (Exception e) {
        throw new IOException(urlString + " - " + conn.getResponseMessage() + "(" + responseCode + ") - "
                + e.getLocalizedMessage());
    }
    return result;
}

From source file:com.ikanow.infinit.e.data_model.driver.InfiniteDriver.java

private String sendPostRequest(String urlAddress, String data, int redirects)
        throws MalformedURLException, IOException {
    String result = "";

    if (urlAddress.startsWith("https:")) {
        TrustManagerManipulator.allowAllSSL();
    }/*  ww  w. j av  a 2  s .c om*/
    URLConnection urlConnection = new URL(urlAddress).openConnection();

    if (cookie != null)
        urlConnection.setRequestProperty("Cookie", cookie);
    if (apiKey != null)
        urlConnection.setRequestProperty("Cookie", apiKey);

    urlConnection.setDoOutput(true);
    urlConnection.setRequestProperty("Accept-Charset", "UTF-8");
    ((HttpURLConnection) urlConnection).setRequestMethod("POST");

    // Post JSON string to URL

    OutputStream os = urlConnection.getOutputStream();

    byte[] b = data.getBytes("UTF-8");

    os.write(b);

    int status = ((HttpURLConnection) urlConnection).getResponseCode();
    // normally, 3xx is redirect
    if (status != HttpURLConnection.HTTP_OK) {
        if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM
                || status == HttpURLConnection.HTTP_SEE_OTHER) {
            if (redirects <= 5) {
                String newUrlAddress = ((HttpURLConnection) urlConnection).getHeaderField("Location");
                if (null != newUrlAddress) {
                    return sendPostRequest(newUrlAddress, data, redirects + 1);
                }
            }
            //(else carry on, will exception out or something below)
        }
    } //TESTED

    // Receive results back from API

    InputStream inStream = urlConnection.getInputStream();

    result = IOUtils.toString(inStream, "UTF-8");

    inStream.close();

    //save cookie if cookie is null
    if (cookie == null) {
        String headername;
        for (int i = 1; (headername = urlConnection.getHeaderFieldKey(i)) != null; i++) {
            if (headername.equals("Set-Cookie")) {
                cookie = urlConnection.getHeaderField(i);
                break;
            }
        }
    }

    return result;
}

From source file:com.ikanow.infinit.e.data_model.driver.InfiniteDriver.java

public String sendGetRequest(String urlAddress, int redirects) throws Exception {
    if (urlAddress.startsWith("https:")) {
        TrustManagerManipulator.allowAllSSL();
    }//from  ww  w  .j  a  v a 2 s . co m

    URL url = new URL(urlAddress);
    URLConnection urlConnection = url.openConnection();
    if (cookie != null)
        urlConnection.setRequestProperty("Cookie", cookie);
    if (apiKey != null)
        urlConnection.setRequestProperty("Cookie", apiKey);

    ((HttpURLConnection) urlConnection).setRequestMethod("GET");

    int status = ((HttpURLConnection) urlConnection).getResponseCode();
    // normally, 3xx is redirect
    if (status != HttpURLConnection.HTTP_OK) {
        if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM
                || status == HttpURLConnection.HTTP_SEE_OTHER) {
            if (redirects <= 5) {
                String newUrlAddress = ((HttpURLConnection) urlConnection).getHeaderField("Location");
                if (null != newUrlAddress) {
                    return sendGetRequest(newUrlAddress, redirects + 1);
                }
            }
            //(else carry on, will exception out or something below)
        }
    } //TESTED

    //read back result
    BufferedReader inStream = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
    StringBuilder strBuilder = new StringBuilder();
    String buffer;
    while ((buffer = inStream.readLine()) != null) {
        strBuilder.append(buffer);
    }
    inStream.close();

    //save cookie if cookie is null
    if (cookie == null) {
        String headername;
        for (int i = 1; (headername = urlConnection.getHeaderFieldKey(i)) != null; i++) {
            if (headername.equals("Set-Cookie")) {
                cookie = urlConnection.getHeaderField(i);
                break;
            }
        }
    }
    return strBuilder.toString();
}

From source file:org.kuali.test.utils.Utils.java

/**
 *
 * @param status/*from  www . j a  v  a  2s . co  m*/
 * @return
 */
public static boolean isHttpRedirect(int status) {
    return ((status == HttpURLConnection.HTTP_MOVED_TEMP) || (status == HttpURLConnection.HTTP_MOVED_PERM)
            || (status == HttpURLConnection.HTTP_SEE_OTHER));
}