Search in sources :

Example 6 with HttpGet

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

the class HttpAsyncClient method download.

/**
 * 下载文件
 *
 * @param url      get提交地址
 * @param callback 回调结果
 */
public static void download(String url, final CallbackVoid<byte[]> callback) {
    // 声明HttpGet对象
    HttpGet get = null;
    try {
        // 获得HttpGet对象
        get = new HttpGet(url);
        get.addHeader(new BasicHeader(HttpConstants.CONTENT_TYPE_KEY, HttpConstants.CONTENT_TYPE_VAL));
        // 执行get
        CLIENT.execute(SimpleRequestBuilder.copy(get).build(), new FutureCallback<SimpleHttpResponse>() {

            @Override
            public void failed(Exception ex) {
                LOG.error(ex);
            }

            @Override
            public void completed(SimpleHttpResponse result) {
                if (callback != null)
                    callback.callback(result.getBodyBytes());
            }

            @Override
            public void cancelled() {
            }
        });
    } catch (Exception e) {
        LOG.error(e);
    }
}
Also used : HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse)

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