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

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

Introduction

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

Prototype

public abstract String getResponseBodyAsString() throws IOException;

Source Link

Usage

From source file:io.fabric8.gateway.apiman.HTTPGatewayApiManTest.java

@Test
public void testShowMappingRequest() throws Exception {
    /** Tests obtaining the mapping info as JSON */
    System.out.println("Mapping requests are not routed through apiman");
    int httpPort = httpGatewayServer.getPort();
    HttpClient httpClient = new HttpClient();
    HttpMethod method = new GetMethod("http://127.0.0.1:" + httpPort + "/");
    assertEquals(200, httpClient.executeMethod(method));
    String content = method.getResponseBodyAsString();
    assertEquals("{\"/hello/world\":[\"http://localhost:18181/root/\"]}", content);
}

From source file:com.gs.jrpip.client.FastServletProxyInvocationHandler.java

private void analyzeServerErrorAndThrow(int code, HttpMethod streamedPostMethod) throws IOException {
    throw new JrpipRuntimeException(
            "Server error (" + code + ").\n" + streamedPostMethod.getResponseBodyAsString());
}

From source file:io.fabric8.gateway.apiman.HTTPGatewayApiManTest.java

@Test
public void testGoldClientRequest() throws Exception {

    int httpPort = httpGatewayServer.getPort();
    HttpClient httpClient = new HttpClient();
    HttpMethod method = new GetMethod("http://127.0.0.1:" + httpPort + "/hello/world?apikey=gold-key");
    assertEquals(200, httpClient.executeMethod(method));
    String content = method.getResponseBodyAsString();
    assertEquals("Hello World!", content);
}

From source file:net.sf.ehcache.constructs.web.filter.GzipFilterTest.java

/**
 * When the servlet container generates a 404 page not found, we want to pass
 * it through without caching and without adding anything to it.
 * <p/>//from w ww.j ava 2 s .c  o m
 * Manual Test: wget -d --server-response --header='Accept-Encoding: gzip'  http://localhost:9080/non_ok/SendRedirectGzip.jsp
 */
public void testRedirect() throws Exception {

    String url = "http://localhost:9080/non_ok/SendRedirectGzip.jsp";
    HttpClient httpClient = new HttpClient();
    HttpMethod httpMethod = new GetMethod(url);
    httpMethod.addRequestHeader("Accept-Encoding", "gzip");
    int responseCode = httpClient.executeMethod(httpMethod);
    //httpclient follows redirects, so gets the home page.
    assertEquals(HttpURLConnection.HTTP_OK, responseCode);
    String responseBody = httpMethod.getResponseBodyAsString();
    assertNotNull(responseBody);
    assertNull(httpMethod.getResponseHeader("Content-Encoding"));
}

From source file:net.sf.ehcache.constructs.web.filter.GzipFilterTest.java

/**
 * When the servlet container forwards to a page does it work?
 * <p/>/*from  ww  w  .j av a2  s .  c o  m*/
 * Manual Test: wget -d --server-response --header='Accept-Encoding: gzip'  http://localhost:9080/non_ok/ForwardFromGzip.jsp
 */
public void testForward() throws Exception {

    String url = "http://localhost:9080/non_ok/ForwardFromGzip.jsp";
    HttpClient httpClient = new HttpClient();
    HttpMethod httpMethod = new GetMethod(url);
    httpMethod.addRequestHeader("Accept-Encoding", "gzip");
    int responseCode = httpClient.executeMethod(httpMethod);
    //httpclient follows redirects, so gets the home page.
    assertEquals(HttpURLConnection.HTTP_OK, responseCode);
    String responseBody = httpMethod.getResponseBodyAsString();
    assertNotNull(responseBody);
    assertEquals("gzip", httpMethod.getResponseHeader("Content-Encoding").getValue());
}

From source file:com.discogs.api.webservice.impl.HttpClientWebService.java

@Override
public Resp doGet(String url) throws WebServiceException {
    HttpMethod method = new GZipCapableGetMethod(url);
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));
    method.setDoAuthentication(true);//www . ja va  2  s . co  m

    try {
        // execute the method
        int statusCode = this.httpClient.executeMethod(method);

        if (logger.isDebugEnabled()) {
            logger.debug(method.getResponseBodyAsString());
        }

        switch (statusCode) {
        case HttpStatus.SC_OK:
            return createResp(method.getResponseBodyAsStream());

        case HttpStatus.SC_NOT_FOUND:
            throw new ResourceNotFoundException("Resource not found.", method.getResponseBodyAsString());

        case HttpStatus.SC_BAD_REQUEST:
            throw new RequestException(method.getResponseBodyAsString());

        case HttpStatus.SC_FORBIDDEN:
            throw new AuthorizationException(method.getResponseBodyAsString());

        case HttpStatus.SC_UNAUTHORIZED:
            throw new AuthorizationException(method.getResponseBodyAsString());

        default:
            String em = "web service returned unknown status '" + statusCode + "', response was: "
                    + method.getResponseBodyAsString();
            logger.error(em);
            throw new WebServiceException(em);
        }
    } catch (HttpException e) {
        logger.error("Fatal protocol violation: " + e.getMessage());
        throw new WebServiceException(e.getMessage(), e);
    } catch (IOException e) {
        logger.error("Fatal transport error: " + e.getMessage());
        throw new WebServiceException(e.getMessage(), e);
    } finally {
        method.releaseConnection();
    }
}

From source file:gov.va.vinci.leo.tools.JamService.java

/**
 * Does the HTTP call and returns the response body as a string, or throws and HttpException.
 *
 * @param method The HttpMethod to call.
 * @return Response body as a string.//from  ww  w .ja  va 2s  .  com
 * @throws IOException  if any communication exception occurs.
 * @throws HttpException If HttpStatus is NOT SC_OK, an HttpException is thrown with the error.
 */
protected String doHttpCall(HttpMethod method) throws IOException, HttpException {
    int statusCode = client.executeMethod(method);
    if (statusCode != HttpStatus.SC_OK) {
        throw new HttpException(
                "Method failed: " + method.getStatusLine() + " Response:" + method.getResponseBodyAsString());
    }
    return method.getResponseBodyAsString();
}

From source file:net.sf.ehcache.constructs.web.filter.GzipFilterTest.java

/**
* When the servlet container generates a 404 page not found, we want to pass
* it through without caching and without adding anything to it.
* <p/>//  ww  w.ja v a2  s.c o  m
* Manual Test: wget -d --server-response --header='Accept-Encoding: gzip'  http://localhost:9080/non_ok/PageNotFoundGzip.jsp
*/
public void testNotFound() throws Exception {

    String url = "http://localhost:9080/non_ok/PageNotFoundGzip.jsp";
    HttpClient httpClient = new HttpClient();
    HttpMethod httpMethod = new GetMethod(url);
    httpMethod.addRequestHeader("If-modified-Since", "Fri, 13 May 3006 23:54:18 GMT");
    httpMethod.addRequestHeader("Accept-Encoding", "gzip");
    int responseCode = httpClient.executeMethod(httpMethod);
    assertEquals(HttpURLConnection.HTTP_NOT_FOUND, responseCode);
    String responseBody = httpMethod.getResponseBodyAsString();
    assertNotNull(responseBody);
    assertNull(httpMethod.getResponseHeader("Content-Encoding"));
}

From source file:com.mobilefirst.fiberlink.WebServiceRequest.java

/**
  * Description: Send Request method/*w ww  . ja v a 2s . co m*/
 * @param method: the type of HTTP Method to send
 * @param responseToVerify: string to verify in the response
 * @return whether the response was verified 
 * @throws Exception
 */
private boolean sendRequest(HttpMethod method, String responseToVerify) throws Exception {
    boolean isResponseVerified = false;
    try {
        statusCode = client.executeMethod(method);
        responseBody = method.getResponseBodyAsString();
        System.out.println("Request URL :: " + method.getURI());
        System.out.println(
                "------------------------------------Begin Debug: Request Headers----------------------------------------------------------\n");
        Header[] requestHeaders = method.getRequestHeaders();
        for (int cn = 0; cn < requestHeaders.length; cn++) {
            System.out.println(requestHeaders[cn].toString());
        }
        System.out.println(
                "------------------------------------Begin Debug: Response Headers----------------------------------------------------------\n");
        Header[] responseHeaders = method.getResponseHeaders();
        for (int cn = 0; cn < responseHeaders.length; cn++) {
            System.out.println(responseHeaders[cn].toString());
        }
        System.out.println(
                "------------------------------------End Debug----------------------------------------------------------\n");
        if (statusCode != HttpStatus.SC_OK) {
            throw new Exception("POST method failed :: " + statusCode + " and ResponseBody :: " + responseBody);
        } else {
            System.out.println(
                    "------------------------------------Response Start----------------------------------------------------------\n");
            System.out.println(responseBody + "\n");
            System.out.println(
                    "------------------------------------Resoonse End----------------------------------------------------------");
            if (null == jsessionId) {
                for (int cnt = 0; cnt < responseHeaders.length; cnt++) {
                    //                  System.out.println(headers[cnt].toString());
                    if (responseHeaders[cnt].toString().contains("Set-Cookie: JSESSIONID=")) {
                        jsessionId = getPatternMatches("JSESSIONID=(.+); Path", responseHeaders[cnt].toString(),
                                false);
                        System.out.println("JESSIONID: " + jsessionId);
                        break;
                    }
                }
            }
            if (responseBody.toLowerCase().contains(responseToVerify.toLowerCase())) {
                System.out.println("RESPONSE VERIFIED. Contains: " + responseToVerify);
                isResponseVerified = true;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("Exception in sendRequest method..." + e.getMessage());
    }
    return isResponseVerified;
}

From source file:com.isencia.passerelle.model.util.RESTFacade.java

private String invokeMethodForURL(HttpMethod method) {
    try {// w  w  w .j  a  va 2s  .c om
        // Execute the method.
        int statusCode = httpClient.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            logger.warn("Response status error : " + method.getStatusLine());
        }

        String response = method.getResponseBodyAsString();
        if (logger.isDebugEnabled()) {
            logger.debug("Received response\n" + response);
        }

        return response;
    } catch (HttpException e) {
        logger.error("Fatal protocol violation: ", e);
        return null;
    } catch (IOException e) {
        logger.error("Fatal transport error: ", e);
        return null;
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
}