Example usage for org.apache.http.client.methods CloseableHttpResponse close

List of usage examples for org.apache.http.client.methods CloseableHttpResponse close

Introduction

In this page you can find the example usage for org.apache.http.client.methods CloseableHttpResponse close.

Prototype

public void close() throws IOException;

Source Link

Document

Closes this stream and releases any system resources associated with it.

Usage

From source file:org.fedoraproject.jenkins.plugins.copr.CoprClient.java

private String doPost(String username, String coprname, List<NameValuePair> params, String url)
        throws IOException {

    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost httppost = new HttpPost(new URL(apiurl, url).toString());

    try {/*w  w  w .j  a v a  2s .com*/
        httppost.setHeader("Authorization", "Basic " + Base64
                .encodeBase64String(String.format("%s:%s", this.apilogin, this.apitoken).getBytes("UTF-8")));
    } catch (UnsupportedEncodingException e) {
        // here goes trouble
        throw new AssertionError(e);
    }

    if (params != null) {
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, Consts.UTF_8);
        httppost.setEntity(entity);
    }

    String result;
    CloseableHttpResponse response = httpclient.execute(httppost);
    result = EntityUtils.toString(response.getEntity());
    response.close();
    httpclient.close();

    return result;
}

From source file:net.oebs.jalos.Client.java

public String get(long id) throws IOException {
    HttpGet httpGet = new HttpGet(this.serviceUrl + "/a/" + id);
    CloseableHttpResponse response = httpClient.execute(httpGet);
    HttpEntity entity = response.getEntity();
    String ret = response.getLastHeader("Location").toString();
    EntityUtils.consume(entity);//  w  ww .j a  v a2  s . c  om
    response.close();
    return ret;
}

From source file:com.collaide.fileuploader.requests.repository.SendFileThread.java

@Override
public void run() {
    try {/*from  w  w  w  .j av  a 2s. com*/
        logger.debug("start sending file");
        CloseableHttpResponse response = httpClient.execute(httpPost, httpContext);
        logger.debug("executing the query...");
        try {
            response.getEntity();
        } finally {
            response.close();
        }
        logger.debug("stop sending file");

    } catch (IOException ex) {
        logger.debug("error while creating a file " + ex);
    }
}

From source file:io.liveoak.keycloak.AuthInterceptorTest.java

private void sendRequestAndCheckStatus(HttpRequestBase req, int expectedStatusCode) throws IOException {
    CloseableHttpResponse resp = httpClient.execute(req);
    assertThat(resp.getStatusLine().getStatusCode()).isEqualTo(expectedStatusCode);
    resp.close();
}

From source file:org.fedoraproject.jenkins.plugins.copr.CoprClient.java

String doGet(String username, String url) throws CoprException {
    CloseableHttpClient httpclient = HttpClients.createDefault();

    HttpGet httpget = new HttpGet(this.apiurl + url);

    try {//from   w  w  w  . j  av a 2  s. co m
        httpget.setHeader("Authorization", "Basic "
                + Base64.encodeBase64String(String.format("%s:%s", apilogin, apitoken).getBytes("UTF-8")));
    } catch (UnsupportedEncodingException e) {
        // here goes trouble
        throw new AssertionError(e);
    }

    String result;
    try {
        CloseableHttpResponse response = httpclient.execute(httpget);
        result = EntityUtils.toString(response.getEntity());
        response.close();
        httpclient.close();
    } catch (IOException e) {
        throw new CoprException("Error while processing HTTP request", e);
    }

    return result;
}

From source file:com.vmware.gemfire.tools.pulse.tests.junit.BaseServiceTest.java

/**
 * Login to pulse server and setup httpClient for tests
 * To be called from setupBeforeClass in each test class
 *///  ww w  .ja  v  a2s. c o  m
protected static void doLogin() throws Exception {
    System.out.println("BaseServiceTest ::  Executing doLogin with user : admin, password : admin.");

    CloseableHttpResponse loginResponse = null;
    try {
        BasicCookieStore cookieStore = new BasicCookieStore();
        httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
        HttpUriRequest login = RequestBuilder.post().setUri(new URI(LOGIN_URL))
                .addParameter("j_username", "admin").addParameter("j_password", "admin").build();
        loginResponse = httpclient.execute(login);
        try {
            HttpEntity entity = loginResponse.getEntity();
            EntityUtils.consume(entity);
            System.out.println("BaseServiceTest :: HTTP request status : " + loginResponse.getStatusLine());

            List<Cookie> cookies = cookieStore.getCookies();
            if (cookies.isEmpty()) {
            } else {
                for (int i = 0; i < cookies.size(); i++) {
                }
            }
        } finally {
            if (loginResponse != null)
                loginResponse.close();
        }
    } catch (Exception failed) {
        logException(failed);
        throw failed;
    }

    System.out.println("BaseServiceTest ::  Executed doLogin");
}

From source file:no.api.meteo.client.DefaultMeteoClient.java

private void closeResponse(CloseableHttpResponse response) {
    try {/*w w  w  .  j  a  va  2s  .  c om*/
        if (response != null) {
            response.close();
        }
    } catch (IOException e) {
        log.warn("Could not close http response. This might cause trouble further down.", e);
    }
}

From source file:com.nibss.util.Request.java

public void get(String url) throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {/*  www  .  j  a  v  a  2s.  c o  m*/
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response1 = httpclient.execute(httpGet);
        try {
            HttpEntity entity1 = response1.getEntity();

            EntityUtils.consume(entity1);
        } finally {
            response1.close();
        }
    } finally {
        httpclient.close();
    }
}

From source file:fr.istic.taa.jaxrs.StatusEndpoint.java

@GET
@Path("/swapi")
@Produces(MediaType.APPLICATION_JSON)/* ww  w .  j  a v  a2  s  .  c o  m*/
public String getSwapi() throws IOException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpget = new HttpGet("http://swapi.co/api/people/1/");
    CloseableHttpResponse response = httpclient.execute(httpget);
    System.out.println(response.toString());
    try {

    } finally {
        response.close();
    }
    return null;

}

From source file:org.wso2.carbon.ml.project.test.GetProjectsTestCase.java

/**
 * Test retrieving all projects.// ww w .java 2  s . co m
 * @throws MLHttpClientException 
 * @throws IOException 
 */
@Test(description = "Retrieve a project")
public void testGetAllProjects() throws MLHttpClientException, IOException {
    CloseableHttpResponse response = mlHttpclient.doHttpGet("/api/projects");
    assertEquals("Unexpected response recieved", Response.Status.OK.getStatusCode(),
            response.getStatusLine().getStatusCode());
    response.close();
}