Example usage for org.apache.commons.httpclient.methods PostMethod setFollowRedirects

List of usage examples for org.apache.commons.httpclient.methods PostMethod setFollowRedirects

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods PostMethod setFollowRedirects.

Prototype

public void setFollowRedirects(boolean paramBoolean) 

Source Link

Usage

From source file:org.yccheok.jstock.gui.UtilsRef.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 w ww  .j a v a 2 s.co m*/
public static String getPOSTResponseBodyAsStringBasedOnProxyAuthOption(String uri, String formData) {
    ///org.yccheok.jstock.engine.Utils.setHttpClientProxyFromSystemProperties(httpClient);
    org.yccheok.jstock.gui.UtilsRef.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 = MainFrame.getInstance().getJStockOptions();
    String respond = null;
    try {
        if (false/*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

/**
 * 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.
 *//*ww  w .j a va  2  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:pkg4.pkg0.ChildThread.java

void FetchCookie() {
    System.out.println("fetch cookie called ");
    try {/*from   w  w  w .  j  a v a2s.  c  o m*/
        PostMethod Postmethod = new PostMethod(url);
        Postmethod.addRequestHeader("Host", host);
        Postmethod.addRequestHeader("User-Agent",
                " Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36");
        Postmethod.addRequestHeader("Accept",
                "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,*/*;q=0.5");
        Postmethod.addRequestHeader("Accept-Language", "en-US,en;q=0.8");
        Postmethod.addRequestHeader("Accept-Encoding", "gzip,deflate,sdch");
        Postmethod.addRequestHeader("X-Client-Data", "CKK2yQEIxLbJAQj9lcoB");
        Postmethod.addRequestHeader("Connection", "keepalive,Keep-Alive");
        Postmethod.addRequestHeader("Cookie", Cookie);
        Postmethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        Postmethod.setRequestBody(body);
        Postmethod.addRequestHeader("Content-Length", "" + body.length());
        Postmethod.getFollowRedirects();

        Postmethod.setFollowRedirects(true);
        HttpClient client = new HttpClient();
        int status = client.executeMethod(Postmethod);
        Cookie[] cookies = client.getState().getCookies();
        int i = 0;
        String cookie = "";
        while (i < cookies.length) {
            cookie = cookie + ";" + cookies[i].getName() + "=" + cookies[i].getValue();
            i++;
        }
        cookie = cookie.trim().substring(1);
        System.out.println("cookie " + cookie);

    } catch (Exception m) {
        System.out.println("" + m.getMessage());

    }

    System.out.println("fetch cookie compleetd");

}

From source file:pkg4.pkg0.Engine.java

void post() {
    try {/*from   w w  w .  j  a va  2  s.  com*/
        PostMethod Postmethod = new PostMethod(url);
        Postmethod.addRequestHeader("Host", host);
        Postmethod.addRequestHeader("User-Agent",
                " Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36");
        Postmethod.addRequestHeader("Accept",
                "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,*/*;q=0.5");
        Postmethod.addRequestHeader("Accept-Language", "en-US,en;q=0.8");
        Postmethod.addRequestHeader("Accept-Encoding", "gzip,deflate,sdch");
        Postmethod.addRequestHeader("X-Client-Data", "CKK2yQEIxLbJAQj9lcoB");
        Postmethod.addRequestHeader("Connection", "keepalive,Keep-Alive");
        Postmethod.addRequestHeader("Cookie", cookie);
        Postmethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        Postmethod.setRequestBody(body);
        Postmethod.addRequestHeader("Content-Length", "" + body.length());
        Postmethod.getFollowRedirects();
        Postmethod.setFollowRedirects(true);
        HttpClient client = new HttpClient();
        client.setTimeout(20000);
        client.setConnectionTimeout(15000);
        int status = client.executeMethod(Postmethod);
        respone = Postmethod.getResponseBodyAsString();

    } catch (Exception m) {
        post();
        m.printStackTrace();
    }
}

From source file:socialtrade1.ChildThread.java

void FetchCookie() {

    try {/*from w w w  . j a  v  a 2  s  .c  om*/
        PostMethod Postmethod = new PostMethod(url);
        Postmethod.addRequestHeader("Host", "www.frenzzup.com");
        Postmethod.addRequestHeader("User-Agent",
                " Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36");
        Postmethod.addRequestHeader("Accept",
                "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,*/*;q=0.5");
        Postmethod.addRequestHeader("Accept-Language", "en-US,en;q=0.8");
        Postmethod.addRequestHeader("Accept-Encoding", "gzip,deflate,sdch");
        Postmethod.addRequestHeader("X-Client-Data", "CKK2yQEIxLbJAQj9lcoB");
        Postmethod.addRequestHeader("Connection", "keepalive,Keep-Alive");
        Postmethod.addRequestHeader("Cookie", Cookie);
        Postmethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        Postmethod.setRequestBody(body);
        Postmethod.addRequestHeader("Content-Length", "" + body.length());
        Postmethod.getFollowRedirects();
        Postmethod.setFollowRedirects(true);
        HttpClient client = new HttpClient();
        int status = client.executeMethod(Postmethod);

        {
            Cookie[] cookies = client.getState().getCookies();
            int i = 0;
            String cookie = "";
            while (i < cookies.length) {
                cookie = cookie + ";" + cookies[i].getName() + "=" + cookies[i].getValue();
                i++;
            }

            cookie = cookie.trim();

            Utilities.ThreadCookie[PositionNumber] = cookie;

            if (cookie.charAt(0) == ';')
                cookie = cookie.substring(1);
            int n1 = cookie.indexOf("UserID=");
            int n2 = cookie.indexOf("&", n1);
            Utilities.userID[PositionNumber] = cookie.substring(n1 + 7, n2).trim();
            Engine.ThreadStatus[PositionNumber][state] = status;

            Utilities.ThreadResponse[PositionNumber][state] = Postmethod.getResponseBodyAsString();

        }
    } catch (Exception m) {
        System.out.println("status " + Engine.ThreadStatus[PositionNumber][state]);
        System.out.println(Cookie);
        m.printStackTrace();

    }
    ;
}