Example usage for org.apache.commons.httpclient HttpStatus SC_SEE_OTHER

List of usage examples for org.apache.commons.httpclient HttpStatus SC_SEE_OTHER

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpStatus SC_SEE_OTHER.

Prototype

int SC_SEE_OTHER

To view the source code for org.apache.commons.httpclient HttpStatus SC_SEE_OTHER.

Click Source Link

Document

<tt>303 See Other</tt> (HTTP/1.1 - RFC 2616)

Usage

From source file:org.yccheok.jstock.gui.UtilsRef.java

public static InputStreamAndMethod getResponseBodyAsStreamBasedOnProxyAuthOption(String request) {
    ///org.yccheok.jstock.engine.Utils.setHttpClientProxyFromSystemProperties(httpClient);
    org.yccheok.jstock.gui.UtilsRef.setHttpClientProxyCredentialsFromJStockOptions(httpClient);

    final GetMethod method = new GetMethod(request);
    ///final JStockOptions jStockOptions = MainFrame.getInstance().getJStockOptions();
    InputStreamAndMethod inputStreamAndMethod = null;
    InputStream respond = null;/*from w w w  .ja  v  a  2  s  .c o  m*/
    HttpMethod methodToClosed = method;

    try {
        if (false/*jStockOptions.isProxyAuthEnabled()*/) {
            method.setFollowRedirects(false);
            httpClient.executeMethod(method);

            int statuscode = method.getStatusCode();
            if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY)
                    || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
                    || (statuscode == HttpStatus.SC_SEE_OTHER)
                    || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
                //Make new Request with new URL
                Header header = method.getResponseHeader("location");
                GetMethod RedirectMethod = new GetMethod(header.getValue());
                methodToClosed = RedirectMethod;
                method.releaseConnection();
                // Do RedirectMethod within try-catch-finally, so that we can have a
                // exception free way to release RedirectMethod connection.
                // #2836422                    
                try {
                    httpClient.executeMethod(RedirectMethod);
                    respond = RedirectMethod.getResponseBodyAsStream();
                } catch (HttpException exp) {
                    log.error(null, exp);
                } catch (IOException exp) {
                    log.error(null, exp);
                }
            } else {
                methodToClosed = method;
                respond = method.getResponseBodyAsStream();
            } // if statuscode = Redirect
        } else {
            methodToClosed = method;
            httpClient.executeMethod(method);
            respond = method.getResponseBodyAsStream();
        } //  if jStockOptions.isProxyAuthEnabled()
    } catch (HttpException exp) {
        log.error(null, exp);
    } catch (IOException exp) {
        log.error(null, exp);
    } finally {
        inputStreamAndMethod = new InputStreamAndMethod(respond, methodToClosed);
    }

    return inputStreamAndMethod;
}

From source file:org.yccheok.jstock.gui.Utils.java

/**
 * Get response body through non-standard POST method.
 * Please refer to <url>http://stackoverflow.com/questions/1473255/is-jakarta-httpclient-sutitable-for-the-following-task/1473305#1473305</url>
 *
 * @param uri For example, http://X/%5bvUpJYKw4QvGRMBmhATUxRwv4JrU9aDnwNEuangVyy6OuHxi2YiY=%5dImage?
 * @param formData For example, [SORT]=0,1,0,10,5,0,KL,0&[FIELD]=33,38,51
 * @return the response body. null if fail.
 *///from ww  w.  j a  v a2 s .co  m
public static String getPOSTResponseBodyAsStringBasedOnProxyAuthOption(String uri, String formData) {
    org.yccheok.jstock.engine.Utils.setHttpClientProxyFromSystemProperties(httpClient);
    org.yccheok.jstock.gui.Utils.setHttpClientProxyCredentialsFromJStockOptions(httpClient);

    final PostMethod method = new PostMethod(uri);
    final RequestEntity entity;
    try {
        entity = new StringRequestEntity(formData, "application/x-www-form-urlencoded", "UTF-8");
    } catch (UnsupportedEncodingException exp) {
        log.error(null, exp);
        return null;
    }
    method.setRequestEntity(entity);
    method.setContentChunked(false);

    final JStockOptions jStockOptions = JStock.instance().getJStockOptions();
    String respond = null;
    try {
        if (jStockOptions.isProxyAuthEnabled()) {
            /* WARNING : This chunck of code block is not tested! */
            method.setFollowRedirects(false);
            httpClient.executeMethod(method);

            int statuscode = method.getStatusCode();
            if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY)
                    || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
                    || (statuscode == HttpStatus.SC_SEE_OTHER)
                    || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
                //Make new Request with new URL
                Header header = method.getResponseHeader("location");
                /* WARNING : Correct method to redirect? Shall we use POST? How about form data? */
                HttpMethod RedirectMethod = new GetMethod(header.getValue());
                // I assume it is OK to release method for twice. (The second
                // release will happen in finally block). We shouldn't have an
                // unreleased method, before executing another new method.
                method.releaseConnection();
                // Do RedirectMethod within try-catch-finally, so that we can have a
                // exception free way to release RedirectMethod connection.
                // #2836422
                try {
                    httpClient.executeMethod(RedirectMethod);
                    respond = RedirectMethod.getResponseBodyAsString();
                } catch (HttpException exp) {
                    log.error(null, exp);
                    return null;
                } catch (IOException exp) {
                    log.error(null, exp);
                    return null;
                } finally {
                    RedirectMethod.releaseConnection();
                }
            } else {
                respond = method.getResponseBodyAsString();
            } // if statuscode = Redirect
        } else {
            httpClient.executeMethod(method);
            respond = method.getResponseBodyAsString();
        } //  if jStockOptions.isProxyAuthEnabled()
    } catch (HttpException exp) {
        log.error(null, exp);
        return null;
    } catch (IOException exp) {
        log.error(null, exp);
        return null;
    } finally {
        method.releaseConnection();
    }
    return respond;
}

From source file:org.yccheok.jstock.gui.Utils.java

private static String _getResponseBodyAsStringBasedOnProxyAuthOption(HttpClient client, String request) {
    org.yccheok.jstock.engine.Utils.setHttpClientProxyFromSystemProperties(client);
    org.yccheok.jstock.gui.Utils.setHttpClientProxyCredentialsFromJStockOptions(client);

    final HttpMethod method = new GetMethod(request);
    final JStockOptions jStockOptions = JStock.instance().getJStockOptions();
    String respond = null;//w  w  w.  j  a v  a2  s .  c o  m
    try {
        if (jStockOptions.isProxyAuthEnabled()) {
            method.setFollowRedirects(false);
            client.executeMethod(method);

            int statuscode = method.getStatusCode();
            if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY)
                    || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
                    || (statuscode == HttpStatus.SC_SEE_OTHER)
                    || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
                //Make new Request with new URL
                Header header = method.getResponseHeader("location");
                HttpMethod RedirectMethod = new GetMethod(header.getValue());
                // I assume it is OK to release method for twice. (The second
                // release will happen in finally block). We shouldn't have an
                // unreleased method, before executing another new method.
                method.releaseConnection();
                // Do RedirectMethod within try-catch-finally, so that we can have a
                // exception free way to release RedirectMethod connection.
                // #2836422
                try {
                    client.executeMethod(RedirectMethod);
                    respond = RedirectMethod.getResponseBodyAsString();
                } catch (HttpException exp) {
                    log.error(null, exp);
                    return null;
                } catch (IOException exp) {
                    log.error(null, exp);
                    return null;
                } finally {
                    RedirectMethod.releaseConnection();
                }
            } else {
                respond = method.getResponseBodyAsString();
            } // if statuscode = Redirect
        } else {
            client.executeMethod(method);
            respond = method.getResponseBodyAsString();
        } //  if jStockOptions.isProxyAuthEnabled()
    } catch (HttpException exp) {
        log.error(null, exp);
        return null;
    } catch (IOException exp) {
        log.error(null, exp);
        return null;
    } finally {
        method.releaseConnection();
    }
    return respond;
}

From source file:org.yccheok.jstock.gui.Utils.java

public static InputStreamAndMethod getResponseBodyAsStreamBasedOnProxyAuthOption(String request) {
    org.yccheok.jstock.engine.Utils.setHttpClientProxyFromSystemProperties(httpClient);
    org.yccheok.jstock.gui.Utils.setHttpClientProxyCredentialsFromJStockOptions(httpClient);

    final GetMethod method = new GetMethod(request);
    final JStockOptions jStockOptions = JStock.instance().getJStockOptions();
    InputStreamAndMethod inputStreamAndMethod = null;
    InputStream respond = null;//  w  w w  .ja va2s .  c o m
    HttpMethod methodToClosed = method;

    try {
        if (jStockOptions.isProxyAuthEnabled()) {
            method.setFollowRedirects(false);
            httpClient.executeMethod(method);

            int statuscode = method.getStatusCode();
            if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY)
                    || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
                    || (statuscode == HttpStatus.SC_SEE_OTHER)
                    || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
                //Make new Request with new URL
                Header header = method.getResponseHeader("location");
                GetMethod RedirectMethod = new GetMethod(header.getValue());
                methodToClosed = RedirectMethod;
                method.releaseConnection();
                // Do RedirectMethod within try-catch-finally, so that we can have a
                // exception free way to release RedirectMethod connection.
                // #2836422                    
                try {
                    httpClient.executeMethod(RedirectMethod);
                    respond = RedirectMethod.getResponseBodyAsStream();
                } catch (HttpException exp) {
                    log.error(null, exp);
                } catch (IOException exp) {
                    log.error(null, exp);
                }
            } else {
                methodToClosed = method;
                respond = method.getResponseBodyAsStream();
            } // if statuscode = Redirect
        } else {
            methodToClosed = method;
            httpClient.executeMethod(method);
            respond = method.getResponseBodyAsStream();
        } //  if jStockOptions.isProxyAuthEnabled()
    } catch (HttpException exp) {
        log.error(null, exp);
    } catch (IOException exp) {
        log.error(null, exp);
    } finally {
        inputStreamAndMethod = new InputStreamAndMethod(respond, methodToClosed);
    }

    return inputStreamAndMethod;
}