Example usage for org.apache.commons.httpclient HttpMethodBase getResponseBodyAsString

List of usage examples for org.apache.commons.httpclient HttpMethodBase getResponseBodyAsString

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpMethodBase getResponseBodyAsString.

Prototype

@Override
public String getResponseBodyAsString() throws IOException 

Source Link

Document

Returns the response body of the HTTP method, if any, as a String .

Usage

From source file:org.paxml.bean.HttpTag.java

/**
 * {@inheritDoc}/*from  www  . j a va  2s .  c o m*/
 */
@Override
protected Object doInvoke(Context context) throws Exception {
    String lowUrl = url.toLowerCase();
    if (!lowUrl.startsWith("http://") && !lowUrl.startsWith("https://")) {
        url = "http://" + url;
    }
    HttpClient client = new HttpClient();
    final HttpMethodBase m;
    if ("post".equalsIgnoreCase(method)) {
        m = setPostBody(new PostMethod(url));
    } else if ("get".equalsIgnoreCase(method)) {
        m = new GetMethod(url);
    } else {
        throw new PaxmlRuntimeException("Unknown method: " + method);
    }
    setHeader(m);
    setQueryString(m);
    // Provide custom retry handler is necessary
    m.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(maxRetry, false));

    onBeforeSend(m);
    // method.setr
    try {
        // Execute the method.

        final int statusCode = client.executeMethod(m);

        if (responseless) {
            return statusCode;
        }

        // Read the response body.
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("code", statusCode);
        result.put("body", m.getResponseBodyAsString());
        result.put("all", m);
        return result;

    } finally {
        // Release the connection.
        m.releaseConnection();
    }
}

From source file:org.wso2.carbon.appfactory.common.util.MutualAuthHttpClient.java

/**
 * Send rest request.//www . j  a  v a  2s  .c  om
 *
 * @param httpClient client object
 * @param method     method type
 * @throws org.wso2.carbon.appfactory.common.AppFactoryException
 */
private static ServerResponse send(HttpClient httpClient, HttpMethodBase method) throws AppFactoryException {
    int responseCode;
    String responseString = null;
    try {
        responseCode = httpClient.executeMethod(method);
    } catch (IOException e) {
        String msg = "Error occurred while executing method " + method.getName();
        log.error(msg, e);
        throw new AppFactoryException(msg, e);
    }
    try {
        responseString = method.getResponseBodyAsString();
    } catch (IOException e) {
        String msg = "error while getting response as String for " + method.getName();
        log.error(msg, e);
        throw new AppFactoryException(msg, e);

    } finally {
        method.releaseConnection();
    }
    if (log.isDebugEnabled()) {
        log.debug("Response id: " + responseCode + " message:  " + responseString);
    }
    return new ServerResponse(responseString, responseCode);
}

From source file:org.wso2.carbon.appfactory.s4.integration.utils.DomainMappingUtils.java

/**
 * Send rest request./*from w w w  .j a  va  2  s  . c o  m*/
 *
 * @param httpClient client object
 * @param method     method type
 * @throws AppFactoryException
 */
private static DomainMappingResponse send(HttpClient httpClient, HttpMethodBase method)
        throws AppFactoryException {
    int responseCode;
    String responseString = null;
    try {
        responseCode = httpClient.executeMethod(method);
    } catch (IOException e) {
        String msg = "Error occurred while executing method " + method.getName();
        log.error(msg, e);
        throw new AppFactoryException(msg, e);
    }
    try {
        responseString = method.getResponseBodyAsString();
    } catch (IOException e) {
        String msg = "error while getting response as String for " + method.getName();
        log.error(msg, e);
        throw new AppFactoryException(msg, e);

    } finally {
        method.releaseConnection();
    }
    if (log.isDebugEnabled()) {
        log.debug(" DomainMappingManagementService response id: " + responseCode + " message:  "
                + responseString);
    }
    return new DomainMappingResponse(responseString, responseCode);
}