Search in sources :

Example 1 with HttpGet

use of org.apache.hc.client5.http.classic.methods.HttpGet in project OpenRefine by OpenRefine.

the class HttpClient method getResponse.

public String getResponse(String urlString, Header[] headers, HttpClientResponseHandler<String> responseHandler) throws IOException {
    try {
        // Use of URL constructor below is purely to get additional error checking to mimic
        // previous behavior for the tests.
        new URL(urlString).toURI();
    } catch (IllegalArgumentException | MalformedURLException | URISyntaxException e) {
        return null;
    }
    HttpGet httpGet = new HttpGet(urlString);
    if (headers != null && headers.length > 0) {
        httpGet.setHeaders(headers);
    }
    // FIXME: Redundant? already includes in client builder
    httpGet.setConfig(defaultRequestConfig);
    return httpClient.execute(httpGet, responseHandler);
}
Also used : MalformedURLException(java.net.MalformedURLException) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) URISyntaxException(java.net.URISyntaxException) URL(java.net.URL)

Example 2 with HttpGet

use of org.apache.hc.client5.http.classic.methods.HttpGet in project metrics by dropwizard.

the class InstrumentedHttpClientsTest method registersExpectedExceptionMetrics.

@Test
public void registersExpectedExceptionMetrics() throws Exception {
    HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0);
    final HttpGet get = new HttpGet("http://localhost:" + httpServer.getAddress().getPort() + "/");
    final String requestMetricName = "request";
    final String exceptionMetricName = "exception";
    httpServer.createContext("/", HttpExchange::close);
    httpServer.start();
    when(metricNameStrategy.getNameFor(any(), any(HttpRequest.class))).thenReturn(requestMetricName);
    when(metricNameStrategy.getNameFor(any(), any(Exception.class))).thenReturn(exceptionMetricName);
    try {
        client.execute(get);
        fail();
    } catch (NoHttpResponseException expected) {
        assertThat(metricRegistry.getMeters()).containsKey("exception");
    } finally {
        httpServer.stop(0);
    }
}
Also used : HttpRequest(org.apache.hc.core5.http.HttpRequest) NoHttpResponseException(org.apache.hc.core5.http.NoHttpResponseException) InetSocketAddress(java.net.InetSocketAddress) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) HttpServer(com.sun.net.httpserver.HttpServer) HttpExchange(com.sun.net.httpserver.HttpExchange) NoHttpResponseException(org.apache.hc.core5.http.NoHttpResponseException) Test(org.junit.Test)

Example 3 with HttpGet

use of org.apache.hc.client5.http.classic.methods.HttpGet in project metrics by dropwizard.

the class InstrumentedHttpClientsTest method registersExpectedMetricsGivenNameStrategy.

@Test
public void registersExpectedMetricsGivenNameStrategy() throws Exception {
    final HttpGet get = new HttpGet("http://example.com?q=anything");
    final String metricName = "some.made.up.metric.name";
    when(metricNameStrategy.getNameFor(any(), any(HttpRequest.class))).thenReturn(metricName);
    client.execute(get);
    verify(registryListener).onTimerAdded(eq(metricName), any(Timer.class));
}
Also used : HttpRequest(org.apache.hc.core5.http.HttpRequest) Timer(com.codahale.metrics.Timer) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) Test(org.junit.Test)

Example 4 with HttpGet

use of org.apache.hc.client5.http.classic.methods.HttpGet in project weicoder by wdcode.

the class HttpClient method download.

/**
 * 下载文件
 *
 * @param  url get提交地址
 * @return     返回流
 */
public static byte[] download(String url) {
    // 声明HttpGet对象
    HttpGet get = null;
    try {
        LOG.debug("HttpClient get url={}", url);
        // 获得HttpGet对象
        get = new HttpGet(url);
        // 获得HttpResponse 返回字节流
        return IOUtil.read(CLIENT.execute(get).getEntity().getContent());
    } catch (Exception e) {
        LOG.error(e);
    } finally {
        // 销毁get
        if (get != null) {
            get.abort();
        }
    }
    return ArrayConstants.BYTES_EMPTY;
}
Also used : HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet)

Example 5 with HttpGet

use of org.apache.hc.client5.http.classic.methods.HttpGet in project weicoder by wdcode.

the class HttpClient5 method download.

@Override
public byte[] download(String url, Map<String, Object> header) {
    // 声明HttpGet对象
    HttpGet get = null;
    try {
        Logs.debug("HttpClient5 get url={}", url);
        // 获得HttpGet对象
        get = new HttpGet(url);
        // 获得HttpResponse 返回字节流
        return IOUtil.read(CLIENT.execute(get).getEntity().getContent());
    } catch (Exception e) {
        Logs.error(e);
    } finally {
        // 销毁get
        if (get != null) {
            get.abort();
        }
    }
    return C.A.BYTES_EMPTY;
}
Also used : HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet)

Aggregations

HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)6 HttpRequest (org.apache.hc.core5.http.HttpRequest)2 Test (org.junit.Test)2 Timer (com.codahale.metrics.Timer)1 HttpExchange (com.sun.net.httpserver.HttpExchange)1 HttpServer (com.sun.net.httpserver.HttpServer)1 InetSocketAddress (java.net.InetSocketAddress)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 SimpleHttpResponse (org.apache.hc.client5.http.async.methods.SimpleHttpResponse)1 NoHttpResponseException (org.apache.hc.core5.http.NoHttpResponseException)1 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)1