Search in sources :

Example 1 with HttpDelete

use of org.apache.hc.client5.http.classic.methods.HttpDelete in project webdrivermanager by bonigarcia.

the class WdmServer method exchange.

public String exchange(String url, String method, String json, int timeoutSec) throws IOException {
    String responseContent = null;
    try (CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build()) {
        HttpUriRequestBase request = null;
        switch(method) {
            case GET:
                request = new HttpGet(url);
                break;
            case DELETE:
                request = new HttpDelete(url);
                break;
            default:
            case POST:
                request = new HttpPost(url);
                HttpEntity body = new StringEntity(json);
                request.setEntity(body);
                request.setHeader("Content-Type", "application/json");
                break;
        }
        RequestConfig requestConfig = custom().setConnectTimeout(timeoutSec, TimeUnit.SECONDS).build();
        request.setConfig(requestConfig);
        try (CloseableHttpResponse response = closeableHttpClient.execute(request)) {
            responseContent = IOUtils.toString(response.getEntity().getContent(), UTF_8);
        }
    }
    return responseContent;
}
Also used : CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) HttpPost(org.apache.hc.client5.http.classic.methods.HttpPost) StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) RequestConfig(org.apache.hc.client5.http.config.RequestConfig) HttpUriRequestBase(org.apache.hc.client5.http.classic.methods.HttpUriRequestBase) HttpDelete(org.apache.hc.client5.http.classic.methods.HttpDelete) HttpEntity(org.apache.hc.core5.http.HttpEntity) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)

Example 2 with HttpDelete

use of org.apache.hc.client5.http.classic.methods.HttpDelete in project geo-platform by geosdi.

the class GPJsonDeleteConnectorRequest method prepareHttpMethod.

/**
 * @return {@link HttpDelete}
 */
@Override
protected HttpDelete prepareHttpMethod() throws Exception {
    String uriPath = super.checkUriPath();
    HttpDelete httpDelete = new HttpDelete(uriPath);
    httpDelete.setConfig(super.prepareRequestConfig());
    return httpDelete;
}
Also used : HttpDelete(org.apache.hc.client5.http.classic.methods.HttpDelete)

Example 3 with HttpDelete

use of org.apache.hc.client5.http.classic.methods.HttpDelete in project spotify-web-api-java by spotify-web-api-java.

the class SpotifyHttpManager method delete.

@Override
public String delete(URI uri, Header[] headers, HttpEntity body) throws IOException, SpotifyWebApiException, ParseException {
    assert (uri != null);
    assert (!uri.toString().equals(""));
    final HttpDelete httpDelete = new HttpDelete(uri);
    httpDelete.setHeaders(headers);
    httpDelete.setEntity(body);
    SpotifyApi.LOGGER.log(Level.FINE, "DELETE request uses these headers: " + GSON.toJson(headers));
    String responseBody = getResponseBody(execute(httpClient, httpDelete));
    httpDelete.reset();
    return responseBody;
}
Also used : HttpDelete(org.apache.hc.client5.http.classic.methods.HttpDelete)

Example 4 with HttpDelete

use of org.apache.hc.client5.http.classic.methods.HttpDelete in project LinkAgent by shulieTech.

the class HttpClientv5MethodInterceptor method getParameters.

private Map getParameters(HttpRequest httpRequest) {
    URI uri = null;
    try {
        uri = httpRequest.getUri();
    } catch (URISyntaxException e) {
        logger.error("获取不到url", e);
    }
    if (httpRequest instanceof HttpGet) {
        HttpGet httpGet = (HttpGet) httpRequest;
        return toMap(uri.getQuery());
    }
    if (httpRequest instanceof HttpPost) {
        HttpPost httpPost = (HttpPost) httpRequest;
        HttpEntity httpEntity = httpPost.getEntity();
        Map parameters = toMap(uri.getQuery());
        InputStream in = null;
        try {
            in = httpEntity.getContent();
            parameters.putAll(toMap(toString(in)));
        } catch (Throwable t) {
        } finally {
            if (in != null) {
                try {
                    in.reset();
                } catch (IOException e) {
                }
            }
        }
        return parameters;
    }
    if (httpRequest instanceof HttpPut) {
        HttpPut httpPut = (HttpPut) httpRequest;
        HttpEntity httpEntity = httpPut.getEntity();
        Map parameters = toMap(uri.getQuery());
        InputStream in = null;
        try {
            in = httpEntity.getContent();
            parameters.putAll(toMap(toString(in)));
        } catch (Throwable t) {
        } finally {
            if (in != null) {
                try {
                    in.reset();
                } catch (IOException e) {
                }
            }
        }
        return parameters;
    }
    if (httpRequest instanceof HttpDelete) {
        HttpDelete httpDelete = (HttpDelete) httpRequest;
        return toMap(uri.getQuery());
    }
    if (httpRequest instanceof HttpHead) {
        HttpHead httpHead = (HttpHead) httpRequest;
        return toMap(uri.getQuery());
    }
    if (httpRequest instanceof HttpOptions) {
        HttpOptions httpOptions = (HttpOptions) httpRequest;
        return toMap(uri.getQuery());
    }
    if (httpRequest instanceof HttpTrace) {
        HttpTrace httpTrace = (HttpTrace) httpRequest;
        return toMap(uri.getQuery());
    }
    if (httpRequest instanceof HttpPatch) {
        HttpPatch httpPatch = (HttpPatch) httpRequest;
        HttpEntity httpEntity = httpPatch.getEntity();
        Map parameters = toMap(uri.getQuery());
        InputStream in = null;
        try {
            in = httpEntity.getContent();
            parameters.putAll(toMap(toString(in)));
        } catch (Throwable t) {
        } finally {
            if (in != null) {
                try {
                    in.reset();
                } catch (IOException e) {
                }
            }
        }
        return parameters;
    }
    return Collections.EMPTY_MAP;
}
Also used : HttpPost(org.apache.hc.client5.http.classic.methods.HttpPost) HttpEntity(org.apache.hc.core5.http.HttpEntity) HttpDelete(org.apache.hc.client5.http.classic.methods.HttpDelete) InputStream(java.io.InputStream) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) HttpOptions(org.apache.hc.client5.http.classic.methods.HttpOptions) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) HttpPut(org.apache.hc.client5.http.classic.methods.HttpPut) HttpHead(org.apache.hc.client5.http.classic.methods.HttpHead) HttpPatch(org.apache.hc.client5.http.classic.methods.HttpPatch) HttpTrace(org.apache.hc.client5.http.classic.methods.HttpTrace) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with HttpDelete

use of org.apache.hc.client5.http.classic.methods.HttpDelete in project wildfly-clustering-spring-session by wildfly-clustering.

the class AbstractSmokeITCase method accept.

@Override
public void accept(URL baseURL1, URL baseURL2) throws Exception {
    URI uri1 = SessionServlet.createURI(baseURL1);
    URI uri2 = SessionServlet.createURI(baseURL2);
    try (CloseableHttpClient client = this.provider.apply(baseURL1, baseURL2)) {
        String sessionId = null;
        int value = 0;
        for (int i = 0; i < 4; i++) {
            for (URI uri : Arrays.asList(uri1, uri2)) {
                for (int j = 0; j < 4; j++) {
                    try (CloseableHttpResponse response = client.execute(new HttpGet(uri))) {
                        Assert.assertEquals(HttpStatus.SC_OK, response.getCode());
                        Assert.assertEquals(String.valueOf(value++), response.getFirstHeader(SessionServlet.VALUE).getValue());
                        String requestSessionId = response.getFirstHeader(SessionServlet.SESSION_ID).getValue();
                        if (sessionId == null) {
                            sessionId = requestSessionId;
                        } else {
                            Assert.assertEquals(sessionId, requestSessionId);
                        }
                    }
                }
                if (!this.transactional) {
                    // Grace time between failover requests
                    Thread.sleep(500);
                }
            }
        }
        try (CloseableHttpResponse response = client.execute(new HttpDelete(uri1))) {
            Assert.assertEquals(HttpStatus.SC_OK, response.getCode());
            Assert.assertEquals(sessionId, response.getFirstHeader(SessionServlet.SESSION_ID).getValue());
        }
        try (CloseableHttpResponse response = client.execute(new HttpHead(uri2))) {
            Assert.assertEquals(HttpStatus.SC_OK, response.getCode());
            String newSessionId = response.containsHeader(SessionServlet.SESSION_ID) ? response.getFirstHeader(SessionServlet.SESSION_ID).getValue() : null;
            Assert.assertNotEquals(sessionId, newSessionId);
        }
    }
}
Also used : CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) HttpDelete(org.apache.hc.client5.http.classic.methods.HttpDelete) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) URI(java.net.URI) HttpHead(org.apache.hc.client5.http.classic.methods.HttpHead)

Aggregations

HttpDelete (org.apache.hc.client5.http.classic.methods.HttpDelete)5 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)3 URI (java.net.URI)2 HttpHead (org.apache.hc.client5.http.classic.methods.HttpHead)2 HttpPost (org.apache.hc.client5.http.classic.methods.HttpPost)2 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)2 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)2 HttpEntity (org.apache.hc.core5.http.HttpEntity)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 HttpOptions (org.apache.hc.client5.http.classic.methods.HttpOptions)1 HttpPatch (org.apache.hc.client5.http.classic.methods.HttpPatch)1 HttpPut (org.apache.hc.client5.http.classic.methods.HttpPut)1 HttpTrace (org.apache.hc.client5.http.classic.methods.HttpTrace)1 HttpUriRequestBase (org.apache.hc.client5.http.classic.methods.HttpUriRequestBase)1 RequestConfig (org.apache.hc.client5.http.config.RequestConfig)1 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)1