Search in sources :

Example 1 with BasicHeader

use of org.apache.hc.core5.http.message.BasicHeader in project weicoder by wdcode.

the class HttpUpload method upload.

/**
 * 上传文件
 *
 * @param  url  post提交地址
 * @param  data 提交参数
 * @param  name 参数名
 * @param  b    流
 * @return      返回结果
 */
public static String upload(String url, Map<String, Object> data, String name, byte[] b) {
    // 如果文件为空
    if (U.E.isEmpty(url) || U.E.isEmpty(b))
        return StringConstants.EMPTY;
    // 声明HttpPost
    HttpPost post = null;
    try {
        // 获得HttpPost
        post = new HttpPost(url);
        post.addHeader(new BasicHeader(HttpConstants.CONTENT_TYPE_KEY, "multipart/form-data"));
        // 多提交实体构造器
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        // 设置浏览器上传
        builder.setMode(HttpMultipartMode.EXTENDED);
        // 添加上传文件
        builder.addBinaryBody(name, b);
        // 参数
        if (U.E.isNotEmpty(data))
            // 设置参数
            data.forEach((k, v) -> builder.addTextBody(k, W.C.toString(v)));
        // 设置提交文件参数
        post.setEntity(builder.build());
        // 返回结果
        return IOUtil.readString(HttpClient.CLIENT.execute(post).getEntity().getContent());
    } catch (Exception e) {
        LOG.error(e);
    } finally {
        // 销毁post
        if (post != null)
            post.abort();
    }
    return StringConstants.EMPTY;
}
Also used : UrlEncodedFormEntity(org.apache.hc.client5.http.entity.UrlEncodedFormEntity) Lists(com.weicoder.common.lang.Lists) Log(com.weicoder.common.log.Log) IOUtil(com.weicoder.common.io.IOUtil) MultipartEntityBuilder(org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder) W(com.weicoder.common.W) File(java.io.File) U(com.weicoder.common.U) HttpPost(org.apache.hc.client5.http.classic.methods.HttpPost) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) List(java.util.List) StringConstants(com.weicoder.common.constants.StringConstants) HttpConstants(com.weicoder.common.constants.HttpConstants) NameValuePair(org.apache.hc.core5.http.NameValuePair) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) Map(java.util.Map) LogFactory(com.weicoder.common.log.LogFactory) HttpMultipartMode(org.apache.hc.client5.http.entity.mime.HttpMultipartMode) HttpPost(org.apache.hc.client5.http.classic.methods.HttpPost) MultipartEntityBuilder(org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder) BasicHeader(org.apache.hc.core5.http.message.BasicHeader)

Example 2 with BasicHeader

use of org.apache.hc.core5.http.message.BasicHeader in project weicoder by wdcode.

the class HttpAsyncClient method post.

/**
 * 模拟post提交
 *
 * @param url      post提交地址
 * @param data     提交参数
 * @param callback 回调结果
 * @param charset  编码
 */
public static void post(String url, Map<String, Object> data, CallbackVoid<String> callback, String charset) {
    // 声明HttpPost
    HttpPost post = null;
    try {
        // 获得HttpPost
        post = new HttpPost(url);
        post.addHeader(new BasicHeader(HttpConstants.CONTENT_TYPE_KEY, HttpConstants.CONTENT_TYPE_VAL));
        // 如果参数列表为空 data为空map
        if (U.E.isNotEmpty(data)) {
            // 声明参数列表
            List<NameValuePair> list = Lists.newList(data.size());
            // 设置参数
            data.forEach((k, v) -> list.add(new BasicNameValuePair(k, W.C.toString(v))));
            // 设置参数与 编码格式
            post.setEntity(new UrlEncodedFormEntity(list));
        }
        // 执行POST
        CLIENT.execute(SimpleRequestBuilder.copy(post).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.getBodyText());
            }

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

Example 3 with BasicHeader

use of org.apache.hc.core5.http.message.BasicHeader in project weicoder by wdcode.

the class HttpClient5 method init.

/**
 * 初始化httpclient
 *
 * @return CloseableHttpClient
 */
private static CloseableHttpClient init() {
    // Http连接池
    PoolingHttpClientConnectionManager pool = new PoolingHttpClientConnectionManager();
    pool.setDefaultMaxPerRoute(C.O.CPU_NUM);
    pool.setMaxTotal(HttpParams.HTTP_MAX);
    // 设置请求参数
    RequestConfig.Builder config = RequestConfig.custom();
    config.setConnectionRequestTimeout(Timeout.ofSeconds(W.C.toLong(HttpParams.HTTP_TIMEOUT)));
    config.setConnectTimeout(Timeout.ofSeconds(W.C.toLong(HttpParams.HTTP_TIMEOUT)));
    config.setCircularRedirectsAllowed(false);
    // HttpClientBuilder
    HttpClientBuilder builder = HttpClientBuilder.create();
    builder.setDefaultRequestConfig(config.build());
    builder.setConnectionManager(pool);
    // builder.setMaxConnPerRoute(SystemConstants.CPU_NUM);
    // 设置 头
    List<BasicHeader> headers = Lists.newList();
    headers.add(new BasicHeader(C.H.USER_AGENT_KEY, C.H.USER_AGENT_VAL));
    headers.add(new BasicHeader(C.H.ACCEPT_KEY, C.H.ACCEPT_VAL));
    builder.setDefaultHeaders(headers);
    // 实例化客户端
    return builder.build();
}
Also used : RequestConfig(org.apache.hc.client5.http.config.RequestConfig) HttpClientBuilder(org.apache.hc.client5.http.impl.classic.HttpClientBuilder) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) PoolingHttpClientConnectionManager(org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager)

Example 4 with BasicHeader

use of org.apache.hc.core5.http.message.BasicHeader in project weicoder by wdcode.

the class HttpClient method init.

/**
 * 初始化httpclient
 *
 * @return CloseableHttpClient
 */
private static CloseableHttpClient init() {
    // Http连接池
    PoolingHttpClientConnectionManager pool = new PoolingHttpClientConnectionManager();
    pool.setDefaultMaxPerRoute(SystemConstants.CPU_NUM);
    pool.setMaxTotal(HttpParams.HTTP_MAX);
    // 设置请求参数
    RequestConfig.Builder config = RequestConfig.custom();
    config.setConnectionRequestTimeout(Timeout.ofSeconds(W.C.toLong(HttpParams.HTTP_TIMEOUT)));
    config.setConnectTimeout(Timeout.ofSeconds(W.C.toLong(HttpParams.HTTP_TIMEOUT)));
    config.setCircularRedirectsAllowed(false);
    // HttpClientBuilder
    HttpClientBuilder builder = HttpClientBuilder.create();
    builder.setDefaultRequestConfig(config.build());
    builder.setConnectionManager(pool);
    // builder.setMaxConnPerRoute(SystemConstants.CPU_NUM);
    // 设置 头
    List<BasicHeader> headers = Lists.newList();
    headers.add(new BasicHeader(HttpConstants.USER_AGENT_KEY, HttpConstants.USER_AGENT_VAL));
    headers.add(new BasicHeader(HttpConstants.ACCEPT_KEY, HttpConstants.ACCEPT_VAL));
    builder.setDefaultHeaders(headers);
    // 实例化客户端
    return builder.build();
}
Also used : RequestConfig(org.apache.hc.client5.http.config.RequestConfig) HttpClientBuilder(org.apache.hc.client5.http.impl.classic.HttpClientBuilder) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) PoolingHttpClientConnectionManager(org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager)

Example 5 with BasicHeader

use of org.apache.hc.core5.http.message.BasicHeader 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

BasicHeader (org.apache.hc.core5.http.message.BasicHeader)6 SimpleHttpResponse (org.apache.hc.client5.http.async.methods.SimpleHttpResponse)2 HttpPost (org.apache.hc.client5.http.classic.methods.HttpPost)2 RequestConfig (org.apache.hc.client5.http.config.RequestConfig)2 UrlEncodedFormEntity (org.apache.hc.client5.http.entity.UrlEncodedFormEntity)2 HttpClientBuilder (org.apache.hc.client5.http.impl.classic.HttpClientBuilder)2 PoolingHttpClientConnectionManager (org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager)2 NameValuePair (org.apache.hc.core5.http.NameValuePair)2 BasicNameValuePair (org.apache.hc.core5.http.message.BasicNameValuePair)2 U (com.weicoder.common.U)1 W (com.weicoder.common.W)1 HttpConstants (com.weicoder.common.constants.HttpConstants)1 StringConstants (com.weicoder.common.constants.StringConstants)1 IOUtil (com.weicoder.common.io.IOUtil)1 Lists (com.weicoder.common.lang.Lists)1 Log (com.weicoder.common.log.Log)1 LogFactory (com.weicoder.common.log.LogFactory)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1