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

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

Introduction

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

Prototype

public abstract byte[] getResponseBody() throws IOException;

Source Link

Usage

From source file:org.zaproxy.zap.extension.ascanrulesAlpha.CloudMetadataScanner.java

void sendMessageWithCustomHostHeader(HttpMessage message, String host) throws IOException {
    HttpMethodParams params = new HttpMethodParams();
    params.setVirtualHost(host);/*from   w  ww  .  j a v a  2s .  c  o  m*/
    HttpMethod method = createRequestMethod(message.getRequestHeader(), message.getRequestBody(), params);
    if (!(method instanceof EntityEnclosingMethod) || method instanceof ZapGetMethod) {
        method.setFollowRedirects(false);
    }
    User forceUser = getParent().getHttpSender().getUser(message);
    message.setTimeSentMillis(System.currentTimeMillis());
    if (forceUser != null) {
        getParent().getHttpSender().executeMethod(method, forceUser.getCorrespondingHttpState());
    } else {
        getParent().getHttpSender().executeMethod(method, null);
    }
    message.setTimeElapsedMillis((int) (System.currentTimeMillis() - message.getTimeSentMillis()));

    HttpMethodHelper.updateHttpRequestHeaderSent(message.getRequestHeader(), method);

    HttpResponseHeader resHeader = HttpMethodHelper.getHttpResponseHeader(method);
    resHeader.setHeader(HttpHeader.TRANSFER_ENCODING, null);
    message.setResponseHeader(resHeader);
    message.getResponseBody().setCharset(resHeader.getCharset());
    message.getResponseBody().setLength(0);
    message.getResponseBody().append(method.getResponseBody());
    message.setResponseFromTargetHost(true);
    getParent().notifyNewMessage(this, message);
}

From source file:poisondog.demo.HttpClientDemo.java

public static void main(String[] args) {
    HttpClientDemo demo = new HttpClientDemo();
    HttpClient client = demo.create("account", "pa5sw0rd");
    HttpMethod method = new GetMethod("http://www.apache.org/");

    try {// www .ja v a 2 s .co  m
        int statusCode = client.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            System.err.println("Method failed: " + method.getStatusLine());
        }
        byte[] responseBody = method.getResponseBody();
        System.out.println(new String(responseBody));
    } catch (HttpException e) {
        System.err.println("Fatal protocol violation: " + e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("Fatal transport error: " + e.getMessage());
        e.printStackTrace();
    } finally {
        method.releaseConnection();
    }
}

From source file:scoap3withapi.Scoap3withAPI.java

public static int getNumRec(String publickey, String privatekey, String date, int jrec, int num_rec) {

    HttpClient client = new HttpClient();
    HttpMethod method = callAPISCOAP3(publickey, privatekey, date, jrec, num_rec);

    double numRec = 0;
    int numFor = 0;
    String responseXML = null;/*from   ww w .ja v  a 2  s . c  o  m*/
    BufferedReader br = null;
    try {
        client.executeMethod(method);
    } catch (IOException ex) {
        Logger.getLogger(Scoap3withAPI.class.getName()).log(Level.SEVERE, null, ex);
    }

    if (method.getStatusCode() == HttpStatus.SC_OK) {
        try {
            method.getResponseBody();

            responseXML = convertStreamToString(method.getResponseBodyAsStream());

            System.out.println("RESPONSE XML " + responseXML);
            numRec = Double.parseDouble(responseXML.split("Results:")[1].split("-->")[0].replace(" ", ""));

            System.out.println("NUM REC=>" + numRec / 100);

            numFor = (int) Math.ceil(numRec / 100);

            System.out.println("NUM REC=>" + numFor);

            method.releaseConnection();

        } catch (IOException ex) {
            Logger.getLogger(Scoap3withAPI.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
    return numFor;
}

From source file:smartrics.rest.client.RestClientImpl.java

/**
 * See {@link smartrics.rest.client.RestClient#execute(java.lang.String, smartrics.rest.client.RestRequest)}
 *///from   w w w .j a  va  2  s. co m
public RestResponse execute(String hostAddr, final RestRequest request) {
    if (request == null || !request.isValid())
        throw new IllegalArgumentException("Invalid request " + request);
    if (request.getTransactionId() == null)
        request.setTransactionId(Long.valueOf(System.currentTimeMillis()));
    LOG.debug("request: {}", request);
    HttpMethod m = createHttpClientMethod(request);
    configureHttpMethod(m, hostAddr, request);
    RestResponse resp = new RestResponse();
    resp.setTransactionId(request.getTransactionId());
    resp.setResource(request.getResource());
    try {
        client.executeMethod(m);
        for (Header h : m.getResponseHeaders()) {
            resp.addHeader(h.getName(), h.getValue());
        }
        resp.setStatusCode(m.getStatusCode());
        resp.setStatusText(m.getStatusText());
        resp.setBody(m.getResponseBodyAsString());
        resp.setRawBody(m.getResponseBody());
    } catch (HttpException e) {
        String message = "Http call failed for protocol failure";
        throw new IllegalStateException(message, e);
    } catch (IOException e) {
        String message = "Http call failed for IO failure";
        throw new IllegalStateException(message, e);
    } finally {
        m.releaseConnection();
    }
    LOG.debug("response: {}", resp);
    return resp;
}

From source file:uk.org.openeyes.oink.facade.ITFacadeRoute.java

@Test
@DirtiesContext/*from  w  ww  . j  a va 2  s. c  om*/
public void testSimplePatientGet() throws Exception {

    /*
     * Set up Third Party Service
     */

    // Specify what the third party service should receive
    IncomingMessageVerifier v = new IncomingMessageVerifier() {
        @Override
        public void isValid(OINKRequestMessage incoming) {
            Assert.assertEquals(uk.org.openeyes.oink.domain.HttpMethod.GET, incoming.getMethod());
            Assert.assertEquals("/Patient/2342452", incoming.getResourcePath());
            Assert.assertNull(incoming.getBody());
        }
    };

    // Specify what the third party service should return
    OINKResponseMessage mockResponse = new OINKResponseMessage();
    mockResponse.setStatus(200);
    mockResponse.setBody(buildFhirBodyFromResource("/example-messages/fhir/patient.json"));

    // Start the third party service
    SimulatedThirdParty thirdp = new SimulatedThirdParty(v, mockResponse);
    thirdp.start();

    /*
     * Make REST request
     */

    // Prepare request
    HttpClient client = new HttpClient();

    HttpMethod method = new GetMethod(testProperties.getProperty("facade.uri") + "/Patient/2342452");

    client.executeMethod(method);
    thirdp.close();

    /*
     * Process REST response
     */
    byte[] responseBody = method.getResponseBody();
    String responseJson = new String(responseBody);
    int responseCode = method.getStatusCode();
    String responseContentType = method.getResponseHeader("Content-Type").getValue();
    method.releaseConnection();

    if (thirdPartyAssertionError != null) {
        throw thirdPartyAssertionError;
    }

    Assert.assertEquals(HttpStatus.SC_OK, responseCode);
    Assert.assertEquals("application/json+fhir", responseContentType);
    Assert.assertEquals(IOUtils.toString(
            this.getClass().getResourceAsStream("/example-messages/fhir/patient.json"), "UTF-8"), responseJson);
}