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:de.xwic.appkit.core.remote.client.URemoteAccessClient.java

/**
 * @param param//from w  w  w . j  a v a 2s .c  o  m
 * @param config
 * @return
 */
public static byte[] postRequest(final IRequestHelper helper, final RemoteSystemConfiguration config) {
    CloseableHttpResponse response = null;
    try {

        CloseableHttpClient client = PoolingHttpConnectionManager.getInstance().getClientInstance(config);
        HttpPost post = new HttpPost(helper.getTargetUrl());
        post.setEntity(helper.getHttpEntity());

        response = client.execute(post);

        int responseCode = response.getStatusLine().getStatusCode();
        if (responseCode != HttpURLConnection.HTTP_OK) {
            throw new RemoteDataAccessException(response.getStatusLine().getReasonPhrase());
        }
        return EntityUtils.toByteArray(response.getEntity());
    } catch (RemoteDataAccessException re) {
        throw re;
    } catch (Exception e) {
        throw new RemoteDataAccessException(e);
    } finally {
        if (response != null) {
            try {
                response.close();
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
    }
}

From source file:shootersubdownloader.Shootersubdownloader.java

private static void down(File f) throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    String url = String.format("https://www.shooter.cn/api/subapi.php?filehash=%s&pathinfo=%s&format=json",
            computefilehash(f), f.getName());
    System.out.println(url);/*  ww w. j av  a  2s  . co m*/
    HttpGet request = new HttpGet(url);
    CloseableHttpResponse r = httpclient.execute(request);
    System.out.println(r.getStatusLine());
    HttpEntity e = r.getEntity();
    String s = EntityUtils.toString(e);
    System.out.println(s);
    JSONArray json = JSONArray.fromObject(s);
    //        JSONObject json = JSONObject.fromObject(s);
    System.out.println(json.size());
    for (int i = 0; i < json.size(); i++) {
        System.out.println(i);
        JSONObject obj = json.getJSONObject(i);
        JSONArray fs = obj.getJSONArray("Files");
        String downurl = fs.getJSONObject(0).getString("Link");
        HttpGet r2 = new HttpGet(downurl);
        CloseableHttpResponse res2 = httpclient.execute(r2);
        //            Header[] headers = res2.getAllHeaders();
        //            for(Header h:headers){
        //                System.out.println(h.getName());
        //                System.out.println(h.getValue());
        //            }
        Header header = res2.getFirstHeader("Content-Disposition");
        String sig = "filename=";
        String v = header.getValue();
        String fn = v.substring(v.indexOf(sig) + sig.length());
        HttpEntity e2 = res2.getEntity();
        File outf = new File(fn);
        FileOutputStream fos = new FileOutputStream(outf);
        e2.writeTo(fos);

        System.out.println(filecharsetdetect.FileCharsetDetect.detect(outf));
        //            res2.getEntity().writeTo(new FileOutputStream(fn));
        System.out.println(fn);
        res2.close();
    }

    r.close();
    httpclient.close();
}

From source file:org.wuspba.ctams.ws.ITSoloResultController.java

protected static void delete() throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();

    URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH)
            .setParameter("id", TestFixture.INSTANCE.soloResult.getId()).build();

    HttpDelete httpDelete = new HttpDelete(uri);

    CloseableHttpResponse response = null;

    try {/*ww  w .j a  v  a 2s . c  o  m*/
        response = httpclient.execute(httpDelete);

        assertEquals(IntegrationTestUtils.OK_STRING, response.getStatusLine().toString());

        HttpEntity responseEntity = response.getEntity();

        EntityUtils.consume(responseEntity);
    } catch (UnsupportedEncodingException ex) {
        LOG.error("Unsupported coding", ex);
    } catch (IOException ioex) {
        LOG.error("IOException", ioex);
    } finally {
        if (response != null) {
            try {
                response.close();
            } catch (IOException ex) {
                LOG.error("Could not close response", ex);
            }
        }
    }

    ITSoloContestController.delete();
}

From source file:tradeok.HttpTool.java

/**
 * HTTPS5S//from w  w  w.j  a  v  a2  s. co  m
 *
 * @param reqURL
 * @param params
 * @return
 */
public static String connectPostHttps(String reqURL, Map<String, String> params) {

    String responseContent = null;

    HttpPost httpPost = new HttpPost(reqURL);
    try {
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(connectTimeout)
                .setConnectTimeout(connectTimeout).setConnectionRequestTimeout(connectTimeout).build();

        List<NameValuePair> formParams = new ArrayList<NameValuePair>();
        httpPost.setEntity(new UrlEncodedFormEntity(formParams, Consts.UTF_8));
        httpPost.setConfig(requestConfig);
        //  Entry
        for (Map.Entry<String, String> entry : params.entrySet()) {
            formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
        CloseableHttpResponse response = httpclient.execute(httpPost);
        try {
            // POST
            HttpEntity entity = response.getEntity(); // ??
            try {
                if (null != entity) {
                    responseContent = EntityUtils.toString(entity, Consts.UTF_8);
                }
            } finally {
                if (entity != null) {
                    entity.getContent().close();
                }
            }
        } finally {
            if (response != null) {
                response.close();
            }
        }
        System.out.printf("\ninfo:requestURI : " + httpPost.getURI() + ", responseContent: " + responseContent);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        System.out.printf("\nerror:ClientProtocolException", e);
    } catch (IOException e) {
        e.printStackTrace();
        System.out.printf("\nerror:IOException", e);
    } finally {
        httpPost.releaseConnection();
    }
    return responseContent;

}

From source file:org.wuspba.ctams.ws.ITBandController.java

public static void delete() throws Exception {
    String id;/*from   ww  w  .  j av  a2s . c o m*/

    CloseableHttpClient httpclient = HttpClients.createDefault();

    URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build();

    HttpGet httpGet = new HttpGet(uri);

    try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
        assertEquals(response.getStatusLine().toString(), IntegrationTestUtils.OK_STRING);

        HttpEntity entity = response.getEntity();

        CTAMSDocument doc = IntegrationTestUtils.convertEntity(entity);

        id = doc.getBands().get(0).getId();

        EntityUtils.consume(entity);
    }

    httpclient = HttpClients.createDefault();

    uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).setParameter("id", id)
            .build();

    HttpDelete httpDelete = new HttpDelete(uri);

    CloseableHttpResponse response = null;

    try {
        response = httpclient.execute(httpDelete);

        assertEquals(IntegrationTestUtils.OK_STRING, response.getStatusLine().toString());

        HttpEntity responseEntity = response.getEntity();

        EntityUtils.consume(responseEntity);
    } catch (UnsupportedEncodingException ex) {
        LOG.error("Unsupported coding", ex);
    } catch (IOException ioex) {
        LOG.error("IOException", ioex);
    } finally {
        if (response != null) {
            try {
                response.close();
            } catch (IOException ex) {
                LOG.error("Could not close response", ex);
            }
        }
    }
}

From source file:org.wuspba.ctams.ws.ITJudgeQualificationController.java

protected static void delete() throws Exception {
    List<String> ids = new ArrayList<>();

    CloseableHttpClient httpclient = HttpClients.createDefault();

    URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build();

    HttpGet httpGet = new HttpGet(uri);

    try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
        assertEquals(response.getStatusLine().toString(), IntegrationTestUtils.OK_STRING);

        HttpEntity entity = response.getEntity();

        CTAMSDocument doc = IntegrationTestUtils.convertEntity(entity);

        for (JudgeQualification q : doc.getJudgeQualifications()) {
            ids.add(q.getId());/*from  www . ja  va2 s.co  m*/
        }

        EntityUtils.consume(entity);
    }

    for (String id : ids) {
        httpclient = HttpClients.createDefault();

        uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH)
                .setParameter("id", id).build();

        HttpDelete httpDelete = new HttpDelete(uri);

        CloseableHttpResponse response = null;

        try {
            response = httpclient.execute(httpDelete);

            assertEquals(IntegrationTestUtils.OK_STRING, response.getStatusLine().toString());

            HttpEntity responseEntity = response.getEntity();

            EntityUtils.consume(responseEntity);
        } catch (UnsupportedEncodingException ex) {
            LOG.error("Unsupported coding", ex);
        } catch (IOException ioex) {
            LOG.error("IOException", ioex);
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException ex) {
                    LOG.error("Could not close response", ex);
                }
            }
        }
    }
}

From source file:functional.PathTraversingTest.java

@Test
public void testTraversalDenied() throws Exception {
    HttpGet httpget = new HttpGet("http://localhost:8080/../../");

    CloseableHttpResponse execute = httpclient.execute(httpget);
    execute.close();
    assertEquals(403, execute.getStatusLine().getStatusCode());
}

From source file:functional.PathTraversingTest.java

@Test
public void testNoTraversal() throws Exception {
    HttpGet httpget = new HttpGet("http://localhost:8080/");

    CloseableHttpResponse execute = httpclient.execute(httpget);
    execute.close();
    assertEquals(200, execute.getStatusLine().getStatusCode());
}

From source file:org.wuspba.ctams.ws.ITJudgeController.java

protected static void delete() throws Exception {
    List<String> ids = new ArrayList<>();

    CloseableHttpClient httpclient = HttpClients.createDefault();

    URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build();

    HttpGet httpGet = new HttpGet(uri);

    try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
        assertEquals(response.getStatusLine().toString(), IntegrationTestUtils.OK_STRING);

        HttpEntity entity = response.getEntity();

        CTAMSDocument doc = IntegrationTestUtils.convertEntity(entity);

        for (Judge j : doc.getJudges()) {
            ids.add(j.getId());/*from   w w  w . ja  v a2  s  .  c  om*/
        }

        EntityUtils.consume(entity);
    }

    for (String id : ids) {
        httpclient = HttpClients.createDefault();

        uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH)
                .setParameter("id", id).build();

        HttpDelete httpDelete = new HttpDelete(uri);

        CloseableHttpResponse response = null;

        try {
            response = httpclient.execute(httpDelete);

            assertEquals(IntegrationTestUtils.OK_STRING, response.getStatusLine().toString());

            HttpEntity responseEntity = response.getEntity();

            EntityUtils.consume(responseEntity);
        } catch (UnsupportedEncodingException ex) {
            LOG.error("Unsupported coding", ex);
        } catch (IOException ioex) {
            LOG.error("IOException", ioex);
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException ex) {
                    LOG.error("Could not close response", ex);
                }
            }
        }
    }

    ITPersonController.delete();
    ITJudgeQualificationController.delete();
}

From source file:org.wuspba.ctams.ws.ITBandResultController.java

protected static void delete() throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();

    URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH)
            .setParameter("id", TestFixture.INSTANCE.bandResult.getId()).build();

    HttpDelete httpDelete = new HttpDelete(uri);

    CloseableHttpResponse response = null;

    try {//from w  w  w . ja  va 2 s . c o  m
        response = httpclient.execute(httpDelete);

        assertEquals(IntegrationTestUtils.OK_STRING, response.getStatusLine().toString());

        HttpEntity responseEntity = response.getEntity();

        EntityUtils.consume(responseEntity);
    } catch (UnsupportedEncodingException ex) {
        LOG.error("Unsupported coding", ex);
    } catch (IOException ioex) {
        LOG.error("IOException", ioex);
    } finally {
        if (response != null) {
            try {
                response.close();
            } catch (IOException ex) {
                LOG.error("Could not close response", ex);
            }
        }
    }

    ITBandController.delete();
    ITBandContestController.delete();
}