Search in sources :

Example 36 with NameValuePair

use of org.apache.hc.core5.http.copied.NameValuePair in project OpenRefine by OpenRefine.

the class HttpClient method postNameValue.

public String postNameValue(String serviceUrl, String name, String value) throws IOException {
    HttpPost request = new HttpPost(serviceUrl);
    List<NameValuePair> body = Collections.singletonList(new BasicNameValuePair(name, value));
    request.setEntity(new UrlEncodedFormEntity(body, StandardCharsets.UTF_8));
    try (CloseableHttpResponse response = httpClient.execute(request)) {
        String reasonPhrase = response.getReasonPhrase();
        int statusCode = response.getCode();
        if (statusCode >= 400) {
            // We should never see 3xx since they get handled automatically
            throw new IOException(String.format("HTTP error %d : %s for URL %s", statusCode, reasonPhrase, request.getRequestUri()));
        }
        return ParsingUtilities.inputStreamToString(response.getEntity().getContent());
    }
}
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) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) UrlEncodedFormEntity(org.apache.hc.client5.http.entity.UrlEncodedFormEntity) IOException(java.io.IOException)

Example 37 with NameValuePair

use of org.apache.hc.core5.http.copied.NameValuePair in project light-4j by networknt.

the class DistinguishedNameParser method parse.

List<NameValuePair> parse(final String s) {
    if (s == null) {
        return null;
    }
    final CharArrayBuffer buffer = new CharArrayBuffer(s.length());
    buffer.append(s);
    final ParserCursor cursor = new ParserCursor(0, s.length());
    return parse(buffer, cursor);
}
Also used : ParserCursor(org.apache.hc.core5.http.message.copied.ParserCursor) CharArrayBuffer(org.apache.hc.core5.util.copied.CharArrayBuffer)

Example 38 with NameValuePair

use of org.apache.hc.core5.http.copied.NameValuePair in project weicoder by wdcode.

the class HttpClient method post.

/**
 * 模拟post提交
 *
 * @param  url     post提交地址
 * @param  data    提交参数
 * @param  header  http头
 * @param  charset 编码
 * @return         提交结果
 */
public static String post(String url, Map<String, String> data, Map<String, String> header, String charset) {
    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));
        LOG.debug("HttpClient post url={} data={} header={} charset={}", url, data, header, charset);
        // 返回结果
        return IOUtil.readString(CLIENT.execute(post).getEntity().getContent());
    } catch (Exception e) {
        LOG.error(e);
    }
    return StringConstants.EMPTY;
}
Also used : StringUtil(com.weicoder.common.util.StringUtil) Maps(com.weicoder.common.lang.Maps) HttpParams(com.weicoder.common.http.params.HttpParams) IOUtil(com.weicoder.common.io.IOUtil) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) W(com.weicoder.common.W) U(com.weicoder.common.U) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) StringConstants(com.weicoder.common.constants.StringConstants) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) Map(java.util.Map) LogFactory(com.weicoder.common.log.LogFactory) BeanUtil(com.weicoder.common.util.BeanUtil) RequestConfig(org.apache.hc.client5.http.config.RequestConfig) SystemConstants(com.weicoder.common.constants.SystemConstants) UrlEncodedFormEntity(org.apache.hc.client5.http.entity.UrlEncodedFormEntity) Lists(com.weicoder.common.lang.Lists) Log(com.weicoder.common.log.Log) HttpClientBuilder(org.apache.hc.client5.http.impl.classic.HttpClientBuilder) JsonEngine(com.weicoder.json.JsonEngine) PoolingHttpClientConnectionManager(org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager) ArrayConstants(com.weicoder.common.constants.ArrayConstants) Timeout(org.apache.hc.core5.util.Timeout) HttpPost(org.apache.hc.client5.http.classic.methods.HttpPost) List(java.util.List) HttpConstants(com.weicoder.common.constants.HttpConstants) NameValuePair(org.apache.hc.core5.http.NameValuePair) StateCode(com.weicoder.common.bean.StateCode) CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) CommonParams(com.weicoder.common.params.CommonParams) 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)

Example 39 with NameValuePair

use of org.apache.hc.core5.http.copied.NameValuePair in project weicoder by wdcode.

the class HttpUpload method upload.

/**
 * 上传文件
 *
 * @param  url   post提交地址
 * @param  data  提交参数
 * @param  files 上传文件
 * @return       返回结果
 */
public static String upload(String url, Map<String, Object> data, File... files) {
    // 如果文件为空
    if (U.E.isEmpty(url) || U.E.isEmpty(files))
        return StringConstants.EMPTY;
    // 声明HttpPost
    HttpPost post = null;
    try {
        // 获得HttpPost
        post = new HttpPost(url);
        // 参数
        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));
        }
        // 多提交实体构造器
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        // 设置浏览器上传
        builder.setMode(HttpMultipartMode.EXTENDED);
        // 添加上传文件
        for (File file : files) builder.addBinaryBody(file.getName(), file);
        // 设置提交文件参数
        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 : HttpPost(org.apache.hc.client5.http.classic.methods.HttpPost) NameValuePair(org.apache.hc.core5.http.NameValuePair) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) MultipartEntityBuilder(org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) UrlEncodedFormEntity(org.apache.hc.client5.http.entity.UrlEncodedFormEntity) File(java.io.File)

Example 40 with NameValuePair

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

NameValuePair (org.apache.hc.core5.http.NameValuePair)56 Test (org.junit.jupiter.api.Test)26 BasicNameValuePair (org.apache.hc.core5.http.message.BasicNameValuePair)25 ArrayList (java.util.ArrayList)15 URI (java.net.URI)13 UrlEncodedFormEntity (org.apache.hc.client5.http.entity.UrlEncodedFormEntity)12 HttpPost (org.apache.hc.client5.http.classic.methods.HttpPost)11 Map (java.util.Map)10 CharArrayBuffer (org.apache.hc.core5.util.CharArrayBuffer)9 IOException (java.io.IOException)7 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)7 URIBuilder (org.apache.hc.core5.net.URIBuilder)7 URISyntaxException (java.net.URISyntaxException)6 HashMap (java.util.HashMap)5 HeaderElement (org.apache.hc.core5.http.HeaderElement)5 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)4 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)4 List (java.util.List)3 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)3 HttpEntity (org.apache.hc.core5.http.HttpEntity)3