Example usage for org.apache.commons.httpclient.params HttpMethodParams SO_TIMEOUT

List of usage examples for org.apache.commons.httpclient.params HttpMethodParams SO_TIMEOUT

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.params HttpMethodParams SO_TIMEOUT.

Prototype

String SO_TIMEOUT

To view the source code for org.apache.commons.httpclient.params HttpMethodParams SO_TIMEOUT.

Click Source Link

Usage

From source file:org.sharegov.cirm.utils.GenUtils.java

@SuppressWarnings("deprecation")
public static String httpPost(String url, String data, String... headers) {
    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod(url);
    if (headers != null) {
        if (headers.length % 2 != 0)
            throw new IllegalArgumentException(
                    "Odd number of headers argument, specify HTTP headers in pairs: name then value, etc.");
        for (int i = 0; i < headers.length; i++)
            method.addRequestHeader(headers[i], headers[++i]);
    }//from  ww  w. java2 s  . co  m
    method.setRequestBody(data);
    try {
        // disable retries from within the HTTP client          
        client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(0, false));
        client.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 0);
        int statusCode = client.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK)
            throw new RuntimeException(
                    "HTTP Error " + statusCode + " while post to " + url.toString() + ", body " + data);
        return method.getResponseBodyAsString();
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        method.releaseConnection();
    }
}

From source file:org.xwiki.test.integration.utils.XWikiExecutor.java

public Response isXWikiStarted(String url, int timeout) throws Exception {
    HttpClient client = new HttpClient();

    boolean connected = false;
    long startTime = System.currentTimeMillis();
    Response response = new Response();
    response.timedOut = false;//ww  w.j  a  v a2  s .c  o  m
    response.responseCode = -1;
    response.responseBody = new byte[0];
    while (!connected && !response.timedOut) {
        GetMethod method = new GetMethod(url);

        // Don't retry automatically since we're doing that in the algorithm below
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(0, false));
        // Set a socket timeout to ensure the server has no chance of not answering to our request...
        method.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, new Integer(10000));

        try {
            // Execute the method.
            response.responseCode = client.executeMethod(method);

            // We must always read the response body.
            response.responseBody = method.getResponseBody();

            if (DEBUG) {
                System.out.println(String.format("Result of pinging [%s] = [%s], Message = [%s]", url,
                        response.responseCode, new String(response.responseBody)));
            }

            // check the http response code is either not an error, either "unauthorized"
            // (which is the case for products that deny view for guest, for example).
            connected = (response.responseCode < 400 || response.responseCode == 401);
        } catch (Exception e) {
            // Do nothing as it simply means the server is not ready yet...
        } finally {
            // Release the connection.
            method.releaseConnection();
        }
        Thread.sleep(500L);
        response.timedOut = (System.currentTimeMillis() - startTime > timeout * 1000L);
    }
    return response;
}

From source file:org.xwiki.test.integration.XWikiExecutor.java

public Response isXWikiStarted(String url, int timeout) throws Exception {
    HttpClient client = new HttpClient();

    boolean connected = false;
    long startTime = System.currentTimeMillis();
    Response response = new Response();
    response.timedOut = false;/*from w  w w  .ja  v  a2s.c  o  m*/
    response.responseCode = -1;
    response.responseBody = new byte[0];
    while (!connected && !response.timedOut) {
        GetMethod method = new GetMethod(url);

        // Don't retry automatically since we're doing that in the algorithm below
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(0, false));
        // Set a socket timeout to ensure the server has no chance of not answering to our request...
        method.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, new Integer(10000));

        try {
            // Execute the method.
            response.responseCode = client.executeMethod(method);

            // We must always read the response body.
            response.responseBody = method.getResponseBody();

            if (DEBUG) {
                System.out.println("Result of pinging [" + url + "] = [" + response.responseCode
                        + "], Message = [" + new String(response.responseBody) + "]");
            }

            // check the http response code is either not an error, either "unauthorized"
            // (which is the case for products that deny view for guest, for example).
            connected = (response.responseCode < 400 || response.responseCode == 401);
        } catch (Exception e) {
            // Do nothing as it simply means the server is not ready yet...
        } finally {
            // Release the connection.
            method.releaseConnection();
        }
        Thread.sleep(500L);
        response.timedOut = (System.currentTimeMillis() - startTime > timeout * 1000L);
    }
    return response;
}