Search in sources :

Example 1 with BasicNameValuePair

use of org.apache.hc.core5.http.message.BasicNameValuePair in project pact-jvm by DiUS.

the class UrlEncocdedFormPostTest method testFormPost.

@Test
void testFormPost(MockServer mockServer) throws IOException {
    HttpResponse httpResponse = Request.post(mockServer.getUrl() + "/form").bodyForm(new BasicNameValuePair("id", UUID.randomUUID().toString()), new BasicNameValuePair("value", "3"), new BasicNameValuePair("value", "1"), new BasicNameValuePair("value", "2")).execute().returnResponse();
    assertThat(httpResponse.getCode(), is(equalTo(200)));
}
Also used : BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) HttpResponse(org.apache.hc.core5.http.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 2 with BasicNameValuePair

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

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

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

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

Aggregations

BasicNameValuePair (org.apache.hc.core5.http.message.BasicNameValuePair)7 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 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