Search in sources :

Example 1 with BasicNameValuePair

use of org.apache.hc.core5.http.message.copied.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.copied.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.copied.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.copied.BasicNameValuePair in project httpcomponents-core by apache.

the class ClassicRequestBuilderTest method builder.

@Test
public void builder() {
    final Header header = new BasicHeader("header2", "blah");
    final ClassicHttpRequest classicHttpRequest = ClassicRequestBuilder.get().setVersion(HttpVersion.HTTP_1_1).setCharset(StandardCharsets.US_ASCII).setAuthority(new URIAuthority("host")).setEntity("<html><body><h1>Access denied</h1></body></html>", ContentType.TEXT_HTML).setHeader(new BasicHeader("header2", "blah")).setHeader("X-Test-Filter", "active").setHeader(header).setPath("path/").setScheme("http").addHeader(header).addHeader("header", ".addHeader(header)").addParameter(new BasicHeader("header2", "blah")).addParameter("param1", "value1").addParameters(new BasicNameValuePair("param3", "value3"), new BasicNameValuePair("param4", null)).setAbsoluteRequestUri(true).setEntity(new StringEntity("requestBody")).setEntity(new ByteArrayEntity(new byte[10240], ContentType.TEXT_PLAIN)).setEntity(new byte[10240], ContentType.TEXT_HTML).setEntity("requestBody").setUri("theUri").setHttpHost(new HttpHost("httpbin.org")).build();
    assertAll("Should return address of Oracle's headquarter", () -> assertNotNull(classicHttpRequest.getEntity()), () -> assertEquals(Method.GET.name(), classicHttpRequest.getMethod()), () -> assertEquals("http", classicHttpRequest.getScheme()), () -> assertEquals("httpbin.org", classicHttpRequest.getAuthority().getHostName()), () -> assertEquals(HttpVersion.HTTP_1_1, classicHttpRequest.getVersion()), () -> assertEquals(4, classicHttpRequest.getHeaders().length), () -> assertNotNull(ClassicRequestBuilder.get().toString()), () -> assertEquals("http://httpbin.org/theUri?header2=blah&param1=value1&param3=value3&param4", new String(classicHttpRequest.getRequestUri().getBytes())));
}
Also used : StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) URIAuthority(org.apache.hc.core5.net.URIAuthority) Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) ByteArrayEntity(org.apache.hc.core5.http.io.entity.ByteArrayEntity) HttpHost(org.apache.hc.core5.http.HttpHost) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Test(org.junit.jupiter.api.Test)

Example 5 with BasicNameValuePair

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

the class TestNameValuePair method testNullValue2NotEqual.

@Test
public void testNullValue2NotEqual() throws Exception {
    final NameValuePair NameValuePair = new BasicNameValuePair("name", "value");
    final NameValuePair NameValuePair2 = new BasicNameValuePair("name", null);
    Assertions.assertNotEquals(NameValuePair, NameValuePair2);
}
Also used : NameValuePair(org.apache.hc.core5.http.NameValuePair) Test(org.junit.jupiter.api.Test)

Aggregations

NameValuePair (org.apache.hc.core5.http.NameValuePair)35 BasicNameValuePair (org.apache.hc.core5.http.message.BasicNameValuePair)29 Test (org.junit.jupiter.api.Test)26 ArrayList (java.util.ArrayList)14 HttpPost (org.apache.hc.client5.http.classic.methods.HttpPost)14 UrlEncodedFormEntity (org.apache.hc.client5.http.entity.UrlEncodedFormEntity)14 Map (java.util.Map)10 IOException (java.io.IOException)9 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)9 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)6 HashMap (java.util.HashMap)5 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)5 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)5 CharArrayBuffer (org.apache.hc.core5.util.CharArrayBuffer)5 List (java.util.List)4 HeaderElement (org.apache.hc.core5.http.HeaderElement)4 ParseException (org.apache.hc.core5.http.ParseException)4 HttpEntity (org.apache.hc.core5.http.HttpEntity)3 U (com.weicoder.common.U)2 W (com.weicoder.common.W)2