Example usage for org.apache.http.impl.client CloseableHttpClient execute

List of usage examples for org.apache.http.impl.client CloseableHttpClient execute

Introduction

In this page you can find the example usage for org.apache.http.impl.client CloseableHttpClient execute.

Prototype

public CloseableHttpResponse execute(final HttpUriRequest request) throws IOException, ClientProtocolException 

Source Link

Usage

From source file:com.oakhole.voa.utils.HttpClientUtils.java

/**
 * ?Get,?/*  w w  w.  ja  va2 s  . co m*/
 *
 * @param uri
 * @param map
 * @return
 */
public static HttpEntity get(String uri, Map<String, String> map) {

    CloseableHttpClient httpClient = HttpClients.createDefault();
    List<NameValuePair> list = Lists.newArrayList();
    for (String key : map.keySet()) {
        list.add(new BasicNameValuePair(key, map.get(key)));
    }

    String param = URLEncodedUtils.format(list, CHARSET);
    String url = uri + "?" + param;
    HttpGet httpGet = new HttpGet(url);
    try {
        CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
        httpResponse.close();
        return httpResponse.getEntity();
    } catch (IOException e) {
        logger.error(":{}", e.getMessage());
    }
    return null;
}

From source file:org.bedework.util.http.HttpUtil.java

public static CloseableHttpResponse doGet(final CloseableHttpClient cl, final URI uri, final Headers hdrs,
        final String acceptContentType) throws IOException {
    final Headers headers = ensureHeaders(hdrs);

    if (acceptContentType != null) {
        headers.add("Accept", acceptContentType);
    }/*  w w w. j ava 2s  .  co  m*/

    final HttpGet httpGet = new HttpGet(uri);

    httpGet.setHeaders(headers.asArray());

    return cl.execute(httpGet);
}

From source file:com.ecofactor.qa.automation.consumerapi.dr.HTTPSClient.java

/**
 * Patch response./*from   w  ww .  jav a  2s .  co  m*/
 *
 * @param url the url
 * @param json the json
 * @param httpClient the http client
 * @return the http response
 */
public static synchronized HttpResponse patchResponse(final String url, final String json,
        final CloseableHttpClient httpClient) {

    try {
        checkHTTPClient(httpClient);
        final HttpPatch httpPatch = new HttpPatch(url);
        httpPatch.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
        httpPatch.setHeader(HttpHeaders.ACCEPT, "application/json");

        final StringEntity params = new StringEntity(json);
        httpPatch.setEntity(params);

        final CloseableHttpResponse response = httpClient.execute(httpPatch);
        setLogString("Status == " + response.getStatusLine(), true);
        return response;

    } catch (IOException | HTTPClientException e) {
        setLogString("Error executing HTTPS method. Reason ::: " + e, true);
        return null;
    }
}

From source file:org.bedework.util.http.HttpUtil.java

public static CloseableHttpResponse doDelete(final CloseableHttpClient cl, final URI uri, final Headers hdrs,
        final String acceptContentType) throws IOException {
    final Headers headers = ensureHeaders(hdrs);

    if (acceptContentType != null) {
        headers.add("Accept", acceptContentType);
    }//  w  ww.j  a  v  a 2  s  . co  m

    final HttpDelete method = new HttpDelete(uri);

    method.setHeaders(headers.asArray());

    return cl.execute(method);
}

From source file:org.bedework.util.http.HttpUtil.java

public static CloseableHttpResponse doHead(final CloseableHttpClient cl, final URI uri, final Headers hdrs,
        final String acceptContentType) throws IOException {
    final Headers headers = ensureHeaders(hdrs);

    if (acceptContentType != null) {
        headers.add("Accept", acceptContentType);
    }/*from w  w w.  j  a v  a  2 s  .  c  o m*/

    final HttpHead httphead = new HttpHead(uri);

    httphead.setHeaders(headers.asArray());

    return cl.execute(httphead);
}

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  ww w  .  j av a  2 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:org.zaproxy.zap.extension.zapwso2jiraplugin.JiraRestClient.java

public static void invokePutMethodWithFile(String auth, String url, String path) {

    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost httppost = new HttpPost(url);
    httppost.setHeader("X-Atlassian-Token", "nocheck");
    httppost.setHeader("Authorization", "Basic " + auth);

    File fileToUpload = new File(path);
    FileBody fileBody = new FileBody(fileToUpload);

    HttpEntity entity = MultipartEntityBuilder.create().addPart("file", fileBody).build();

    httppost.setEntity(entity);/*www  . j ava 2  s.com*/
    CloseableHttpResponse response = null;

    try {
        response = httpclient.execute(httppost);
    } catch (Exception e) {
        log.error("File upload failed when involing the update method with file ");
    } finally {
        try {
            httpclient.close();
        } catch (IOException e) {
            log.error("Exception occered when closing the connection");
        }
    }
}

From source file:org.wso2.am.integration.test.utils.http.HTTPSClientUtils.java

/**
 * POST function implementation/*w w  w  .  ja  v  a 2s.c om*/
 *
 * @param httpClient    http client to use
 * @param url           request URL
 * @param headers       headers to be send
 * @param urlParameters parameters to be sent as payload
 * @return org.apache.http.HttpResponse
 * @throws IOException if connection issue occurred
 */
private static HttpResponse sendPOSTMessage(CloseableHttpClient httpClient, String url,
        Map<String, String> headers, List<NameValuePair> urlParameters) throws IOException {
    HttpPost post = new HttpPost(url);
    if (headers != null) {
        for (Map.Entry<String, String> head : headers.entrySet()) {
            post.addHeader(head.getKey(), head.getValue());
        }
    }
    post.setEntity(new UrlEncodedFormEntity(urlParameters));
    return httpClient.execute(post);
}

From source file:org.openo.nfvo.emsdriver.northbound.client.HttpClientUtil.java

public static String doPost(String url, String json, String charset) {
    CloseableHttpClient httpClient = null;
    HttpPost httpPost = null;/*from  w ww  . jav  a2  s .c om*/
    String result = null;
    try {
        httpClient = HttpClientFactory.getSSLClientFactory();
        httpPost = new HttpPost(url);
        if (null != json) {
            StringEntity s = new StringEntity(json);
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json"); // set contentType
            httpPost.setEntity(s);
        }
        CloseableHttpResponse response = httpClient.execute(httpPost);
        try {
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, charset);
                }
            }
        } catch (Exception e) {
            log.error("httpClient.execute(httpPost) is fail", e);
        } finally {
            if (response != null) {
                response.close();
            }
        }
    } catch (Exception e) {
        log.error("doPost is fail ", e);
    } finally {
        if (httpClient != null) {
            try {
                httpClient.close();
            } catch (IOException e) {
            }
        }

    }
    return result;
}

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.java 2s  .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);
                }
            }
        }
    }

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