Search in sources :

Example 1 with NameValuePair

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

the class ApacheHttp5Client method toClassicHttpRequest.

ClassicHttpRequest toClassicHttpRequest(Request request, Request.Options options) throws URISyntaxException {
    final ClassicRequestBuilder requestBuilder = ClassicRequestBuilder.create(request.httpMethod().name());
    final URI uri = new URIBuilder(request.url()).build();
    requestBuilder.setUri(uri.getScheme() + "://" + uri.getAuthority() + uri.getRawPath());
    // request query params
    final List<NameValuePair> queryParams = URLEncodedUtils.parse(uri, requestBuilder.getCharset());
    for (final NameValuePair queryParam : queryParams) {
        requestBuilder.addParameter(queryParam);
    }
    // request headers
    boolean hasAcceptHeader = false;
    for (final Map.Entry<String, Collection<String>> headerEntry : request.headers().entrySet()) {
        final String headerName = headerEntry.getKey();
        if (headerName.equalsIgnoreCase(ACCEPT_HEADER_NAME)) {
            hasAcceptHeader = true;
        }
        if (headerName.equalsIgnoreCase(Util.CONTENT_LENGTH)) {
            // doesn't like us to set it as well.
            continue;
        }
        for (final String headerValue : headerEntry.getValue()) {
            requestBuilder.addHeader(headerName, headerValue);
        }
    }
    // some servers choke on the default accept string, so we'll set it to anything
    if (!hasAcceptHeader) {
        requestBuilder.addHeader(ACCEPT_HEADER_NAME, "*/*");
    }
    // request body
    // final Body requestBody = request.requestBody();
    byte[] data = request.body();
    if (data != null) {
        HttpEntity entity;
        if (request.isBinary()) {
            entity = new ByteArrayEntity(data, null);
        } else {
            final ContentType contentType = getContentType(request);
            entity = new StringEntity(new String(data), contentType);
        }
        requestBuilder.setEntity(entity);
    } else {
        requestBuilder.setEntity(new ByteArrayEntity(new byte[0], null));
    }
    return requestBuilder.build();
}
Also used : URI(java.net.URI) URIBuilder(org.apache.hc.core5.net.URIBuilder) StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) ByteArrayEntity(org.apache.hc.core5.http.io.entity.ByteArrayEntity) ClassicRequestBuilder(org.apache.hc.core5.http.io.support.ClassicRequestBuilder)

Example 2 with NameValuePair

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

the class HttpUtils method isValidCaptcha.

/**
 * @param token CAPTCHA
 * @return boolean
 */
public static boolean isValidCaptcha(String token) {
    if (StringUtils.isBlank(Config.getConfigParam("signup_captcha_secret_key", ""))) {
        return true;
    }
    if (StringUtils.isBlank(token)) {
        return false;
    }
    List<NameValuePair> params = new ArrayList<>();
    params.add(new BasicNameValuePair("secret", Config.getConfigParam("signup_captcha_secret_key", "")));
    params.add(new BasicNameValuePair("response", token));
    HttpPost post = new HttpPost("https://www.google.com/recaptcha/api/siteverify");
    post.setEntity(new UrlEncodedFormEntity(params));
    try (CloseableHttpResponse resp = HttpUtils.getHttpClient().execute(post)) {
        if (resp.getCode() == HttpStatus.SC_OK && resp.getEntity() != null) {
            Map<String, Object> data = ParaObjectUtils.getJsonReader(Map.class).readValue(resp.getEntity().getContent());
            if (data != null && data.containsKey("success")) {
                return (boolean) data.getOrDefault("success", false);
            }
        }
    } catch (Exception ex) {
        LoggerFactory.getLogger(HttpUtils.class).debug("Failed to verify CAPTCHA: {}", ex.getMessage());
    }
    return false;
}
Also used : BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) NameValuePair(org.apache.hc.core5.http.NameValuePair) HttpPost(org.apache.hc.client5.http.classic.methods.HttpPost) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) UrlEncodedFormEntity(org.apache.hc.client5.http.entity.UrlEncodedFormEntity) Map(java.util.Map) IOException(java.io.IOException)

Example 3 with NameValuePair

use of org.apache.hc.core5.http.copied.NameValuePair 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 4 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 CharArrayBuffer buf, final ParserCursor cursor) {
    final List<NameValuePair> params = new ArrayList<>();
    tokenParser.skipWhiteSpace(buf, cursor);
    while (!cursor.atEnd()) {
        final NameValuePair param = parseParameter(buf, cursor);
        params.add(param);
    }
    return params;
}
Also used : BasicNameValuePair(org.apache.hc.core5.http.message.copied.BasicNameValuePair) NameValuePair(org.apache.hc.core5.http.copied.NameValuePair) ArrayList(java.util.ArrayList)

Example 5 with NameValuePair

use of org.apache.hc.core5.http.copied.NameValuePair in project httpcomponents-core by apache.

the class FrameworkTest method moveAnyParametersInPathToQuery.

private void moveAnyParametersInPathToQuery(final Map<String, Object> request) throws TestingFrameworkException {
    try {
        final String path = (String) request.get(PATH);
        if (path != null) {
            final URI uri = path.startsWith("/") ? new URI("http://localhost:8080" + path) : new URI("http://localhost:8080/");
            final URIBuilder uriBuilder = new URIBuilder(uri, StandardCharsets.UTF_8);
            final List<NameValuePair> params = uriBuilder.getQueryParams();
            @SuppressWarnings("unchecked") final Map<String, Object> queryMap = (Map<String, Object>) request.get(QUERY);
            for (final NameValuePair param : params) {
                queryMap.put(param.getName(), param.getValue());
            }
            if (!params.isEmpty()) {
                request.put(PATH, uri.getPath());
            }
        }
    } catch (final URISyntaxException e) {
        throw new TestingFrameworkException(e);
    }
}
Also used : NameValuePair(org.apache.hc.core5.http.NameValuePair) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) HashMap(java.util.HashMap) Map(java.util.Map) URIBuilder(org.apache.hc.core5.net.URIBuilder)

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