Search in sources :

Example 6 with UrlEncodedFormEntity

use of org.apache.hc.client5.http.entity.UrlEncodedFormEntity in project weicoder by wdcode.

the class HttpClient5 method post.

@Override
public String post(String url, Map<String, Object> data, Map<String, Object> header) {
    try {
        // 获得HttpPost
        HttpPost post = new HttpPost(url);
        // 如果参数列表为空 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));
        }
        // 添加http头
        if (U.E.isNotEmpty(header))
            header.forEach((k, v) -> post.addHeader(k, v));
        Logs.debug("HttpClient post url={} data={} header={}", url, data, header);
        // 返回结果
        return U.I.readString(CLIENT.execute(post).getEntity().getContent());
    } catch (Exception e) {
        Logs.error(e);
    }
    return C.S.EMPTY;
}
Also used : UrlEncodedFormEntity(org.apache.hc.client5.http.entity.UrlEncodedFormEntity) BaseHttp(com.weicoder.common.http.base.BaseHttp) Lists(com.weicoder.common.lang.Lists) HttpClientBuilder(org.apache.hc.client5.http.impl.classic.HttpClientBuilder) HttpParams(com.weicoder.common.http.params.HttpParams) IOUtil(com.weicoder.common.io.IOUtil) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) PoolingHttpClientConnectionManager(org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager) W(com.weicoder.common.W) Timeout(org.apache.hc.core5.util.Timeout) U(com.weicoder.common.U) HttpPost(org.apache.hc.client5.http.classic.methods.HttpPost) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) C(com.weicoder.common.C) List(java.util.List) NameValuePair(org.apache.hc.core5.http.NameValuePair) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) Map(java.util.Map) RequestConfig(org.apache.hc.client5.http.config.RequestConfig) CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) Logs(com.weicoder.common.log.Logs) HttpPost(org.apache.hc.client5.http.classic.methods.HttpPost) NameValuePair(org.apache.hc.core5.http.NameValuePair) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) UrlEncodedFormEntity(org.apache.hc.client5.http.entity.UrlEncodedFormEntity)

Aggregations

HttpPost (org.apache.hc.client5.http.classic.methods.HttpPost)6 UrlEncodedFormEntity (org.apache.hc.client5.http.entity.UrlEncodedFormEntity)6 NameValuePair (org.apache.hc.core5.http.NameValuePair)6 BasicNameValuePair (org.apache.hc.core5.http.message.BasicNameValuePair)6 Map (java.util.Map)3 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)3 U (com.weicoder.common.U)2 W (com.weicoder.common.W)2 HttpParams (com.weicoder.common.http.params.HttpParams)2 IOUtil (com.weicoder.common.io.IOUtil)2 Lists (com.weicoder.common.lang.Lists)2 IOException (java.io.IOException)2 List (java.util.List)2 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)2 RequestConfig (org.apache.hc.client5.http.config.RequestConfig)2 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)2 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)2 HttpClientBuilder (org.apache.hc.client5.http.impl.classic.HttpClientBuilder)2 PoolingHttpClientConnectionManager (org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager)2 Timeout (org.apache.hc.core5.util.Timeout)2