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:com.tasktop.c2c.server.profile.tests.service.ProfileWebServiceClientTest.java

@Test
public void serviceAcceptsNonCompliantJSONMediaTypes() throws HttpException, IOException {
    String baseUrl = profileWebServiceClient.getBaseUrl();
    for (String mediaType : new String[] { "application/json", "application/x-javascript", "text/javascript",
            "text/x-javascript", "text/x-json", "text/json" }) {
        HttpClient client = new HttpClient();
        HttpMethod get = new GetMethod(baseUrl + "/"
                + ProfileWebServiceClient.GET_PROJECT_BY_IDENTIFIER_URL.replaceAll("\\{.*?\\}", "123"));
        get.addRequestHeader("Accept", mediaType);
        int rc = client.executeMethod(get);
        String responseBody = get.getResponseBodyAsString();

        assertTrue("Expected JSON response for media type \"" + mediaType + "\" but got " + responseBody,
                responseBody.trim().startsWith("{\"error\":{\"message\":"));
    }/* w  w w  . j a  v a  2  s  .  co  m*/
}

From source file:eionet.eea.template.RefreshTemplateServlet.java

/**
 * Reads the URL and returns the content.
 * Pair id - HTTP status code, Pair value - response body.
 *
 * @param url url to check./*from  w  w w . j ava2 s  .c o  m*/
 * @return
 */
private Pair<Integer, String> readContentFromUrl(String url) {
    Pair<Integer, String> result = null;

    if (StringUtils.isNotBlank(url)) {
        try {
            HttpClient client = new HttpClient();
            HttpMethod get = new GetMethod(url);

            client.executeMethod(get);
            result = new Pair<Integer, String>();
            result.setId(get.getStatusCode());
            if (get.getStatusCode() != 404) {
                result.setValue(get.getResponseBodyAsString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return result;
}

From source file:ccc.api.http.SiteBrowserImpl.java

private String invoke(final HttpMethod m) {
    try {//w  w  w  . j a va 2 s  .c  o  m
        _httpClient.executeMethod(m);
        final int status = m.getStatusCode();
        if (OK == status) {
            return m.getResponseBodyAsString();
        }
        throw new RuntimeException(status + ": " + m.getResponseBodyAsString());
    } catch (final HttpException e) {
        throw new InternalError(); // FIXME: Report error.
    } catch (final IOException e) {
        throw new InternalError(); // FIXME: Report error.
    } finally {
        m.releaseConnection();
    }
}

From source file:info.jtrac.mylyn.JtracClient.java

private String doGet(String url) throws Exception {
    HttpMethod get = new GetMethod(url);
    String response = null;/*from w  w w. j  av  a  2s.c  o  m*/
    int code;
    try {
        code = httpClient.executeMethod(get);
        if (code != HttpURLConnection.HTTP_OK) {
            throw new HttpException("HTTP Response Code: " + code);
        }
        response = get.getResponseBodyAsString();
    } finally {
        get.releaseConnection();
    }
    return response;
}

From source file:com.bt.aloha.batchtest.v2.RemoteTomcatStackRunner.java

private String getResponse(String uri) {
    HttpClient httpClient = new HttpClient();
    httpClient.getState().setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(getUsername(), getPassword()));
    HttpMethod getMethod = new GetMethod(uri);
    getMethod.setDoAuthentication(true);

    try {//from  w  ww . j  a  va 2  s  . c  o m
        int rc = httpClient.executeMethod(getMethod);
        LOG.debug(rc);
        if (rc != 200)
            throw new RuntimeException(String.format("bad http response from Tomcat: %d", rc));
        return getMethod.getResponseBodyAsString();
    } catch (HttpException e) {
        LOG.warn(e);
        throw new RuntimeException(e);
    } catch (IOException e) {
        LOG.warn(e);
        throw new RuntimeException(e);
    }
}

From source file:com.alibaba.intl.bcds.goldroom.remote.DoubanBookInfoFetcher.java

public BookInfo fetch(String isbn) {
    HttpMethod method = new GetMethod(fetchUrl + isbn);
    HttpClient client = new HttpClient();
    try {//from  w ww  . j  a va2  s .  c  o  m
        client.executeMethod(method);
        BookInfo info = parserBookInfo(method.getResponseBodyAsString());
        return info;

    } catch (HttpException e) {
        logger.error("fetch bookinfo whth isbn(" + isbn + ") error", e);
    } catch (Exception e) {
        logger.error("fetch bookinfo whth isbn(" + isbn + ") error", e);
    }
    return null;
}

From source file:io.aino.agents.core.AgentIntegrationTest.java

@Test
public void ainoMockApiIsRunningTest() throws Exception {
    HttpMethod get = new GetMethod(API_URL + "/ping");

    int statusCode = client.executeMethod(get);

    assertEquals(200, statusCode);//  w  w  w.  ja  v a  2s  . c  om
    assertEquals("pong", get.getResponseBodyAsString().trim());
}

From source file:com.jdom.word.playdough.model.ServerCommunicationManagerImpl.java

public int getUserHighScoreForWord(String user, String word) {
    if (configuration.isCommunicateWithServer()) {
        HttpClient client = getHttpClient();
        HttpMethod method = getHttpMethod(Constants.GET_HIGH_SCORE_URL + "?" + Constants.USER_KEY + "=" + user
                + "&" + Constants.WORD_KEY + "=" + word);

        try {/*from   w  w  w . ja v a 2 s .co  m*/
            client.executeMethod(method);
            return Integer.parseInt(method.getResponseBodyAsString());
        } catch (Exception e) {
            return cacheStatusMarker.getCachedUserHighScore(word);
        } finally {
            method.releaseConnection();
        }
    } else {
        return cacheStatusMarker.getCachedUserHighScore(word);
    }
}

From source file:com.jdom.word.playdough.model.ServerCommunicationManagerImpl.java

public boolean isGamePackLocked(String username, String gamePackFileName) {
    if (configuration.isCommunicateWithServer()) {
        HttpClient client = getHttpClient();
        HttpMethod method = getHttpMethod(Constants.GET_LOCK_STATUS_URL + "?" + Constants.USER_KEY + "="
                + username + "&" + Constants.PACK_KEY + "=" + gamePackFileName);

        try {//from  w w w .  j  av a  2  s  .com
            client.executeMethod(method);
            return Boolean.parseBoolean(method.getResponseBodyAsString());
        } catch (Exception e) {
            return cacheStatusMarker.isGamePackLocked(username, gamePackFileName);
        } finally {
            method.releaseConnection();
        }
    } else {
        return cacheStatusMarker.isGamePackLocked(username, gamePackFileName);
    }
}

From source file:com.uber.jenkins.phabricator.uberalls.UberallsClient.java

public String getCoverage(String sha) {
    URIBuilder builder;// w w w  .j a  va2  s  .  com
    try {
        builder = getBuilder().setParameter("sha", sha).setParameter("repository", repository);

        HttpClient client = getClient();
        HttpMethod request = new GetMethod(builder.build().toString());
        int statusCode = client.executeMethod(request);

        if (statusCode != HttpStatus.SC_OK) {
            logger.info(TAG, "Call failed: " + request.getStatusLine());
            return null;
        }
        return request.getResponseBodyAsString();
    } catch (HttpResponseException e) {
        if (e.getStatusCode() != 404) {
            e.printStackTrace(logger.getStream());
        }
    } catch (Exception e) {
        e.printStackTrace(logger.getStream());
    }
    return null;
}