Search in sources :

Example 21 with NameValuePair

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

the class TestEntityUtils method testParseUTF8Entity.

@Test
public void testParseUTF8Entity() throws Exception {
    final String ru_hello = constructString(RUSSIAN_HELLO);
    final String ch_hello = constructString(SWISS_GERMAN_HELLO);
    final List<NameValuePair> parameters = new ArrayList<>();
    parameters.add(new BasicNameValuePair("russian", ru_hello));
    parameters.add(new BasicNameValuePair("swiss", ch_hello));
    final String s = WWWFormCodec.format(parameters, StandardCharsets.UTF_8);
    Assertions.assertEquals("russian=%D0%92%D1%81%D0%B5%D0%BC_%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82" + "&swiss=Gr%C3%BCezi_z%C3%A4m%C3%A4", s);
    final StringEntity entity = new StringEntity(s, ContentType.APPLICATION_FORM_URLENCODED.withCharset(StandardCharsets.UTF_8));
    final List<NameValuePair> result = EntityUtils.parse(entity);
    Assertions.assertEquals(2, result.size());
    assertNameValuePair(result.get(0), "russian", ru_hello);
    assertNameValuePair(result.get(1), "swiss", ch_hello);
}
Also used : NameValuePair(org.apache.hc.core5.http.NameValuePair) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 22 with NameValuePair

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

the class AsyncRequestBuilder method build.

@Override
public AsyncRequestProducer build() {
    String path = getPath();
    if (TextUtils.isEmpty(path)) {
        path = "/";
    }
    AsyncEntityProducer entityProducerCopy = entityProducer;
    final String method = getMethod();
    final List<NameValuePair> parameters = getParameters();
    if (parameters != null && !parameters.isEmpty()) {
        final Charset charset = getCharset();
        if (entityProducerCopy == null && (Method.POST.isSame(method) || Method.PUT.isSame(method))) {
            final String content = WWWFormCodec.format(parameters, charset != null ? charset : ContentType.APPLICATION_FORM_URLENCODED.getCharset());
            entityProducerCopy = new StringAsyncEntityProducer(content, ContentType.APPLICATION_FORM_URLENCODED);
        } else {
            try {
                final URI uri = new URIBuilder(path).setCharset(charset).addParameters(parameters).build();
                path = uri.toASCIIString();
            } catch (final URISyntaxException ex) {
            // should never happen
            }
        }
    }
    if (entityProducerCopy != null && Method.TRACE.isSame(method)) {
        throw new IllegalStateException(Method.TRACE + " requests may not include an entity");
    }
    final BasicHttpRequest request = new BasicHttpRequest(method, getScheme(), getAuthority(), path);
    request.setVersion(getVersion());
    request.setHeaders(getHeaders());
    request.setAbsoluteRequestUri(isAbsoluteRequestUri());
    return new BasicRequestProducer(request, entityProducerCopy);
}
Also used : NameValuePair(org.apache.hc.core5.http.NameValuePair) StringAsyncEntityProducer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer) BasicAsyncEntityProducer(org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer) AsyncEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer) StringAsyncEntityProducer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer) Charset(java.nio.charset.Charset) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) URIBuilder(org.apache.hc.core5.net.URIBuilder)

Example 23 with NameValuePair

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

the class EntityUtils method parse.

/**
 * Returns a list of {@link NameValuePair NameValuePairs} as parsed from an {@link HttpEntity}.
 * The encoding is taken from the entity's Content-Encoding header.
 * <p>
 * This is typically used while parsing an HTTP POST.
 * </p>
 *
 * @param entity
 *            The entity to parse
 * @param maxStreamLength
 *            The maximum size of the stream to read; use it to guard against unreasonable or malicious processing.
 * @return a list of {@link NameValuePair} as built from the URI's query portion.
 * @throws IOException
 *             If there was an exception getting the entity's data.
 */
public static List<NameValuePair> parse(final HttpEntity entity, final int maxStreamLength) throws IOException {
    Args.notNull(entity, "HttpEntity");
    final int contentLength = toContentLength((int) Args.checkContentLength(entity));
    final ContentType contentType = ContentType.parse(entity.getContentType());
    if (!ContentType.APPLICATION_FORM_URLENCODED.isSameMimeType(contentType)) {
        return Collections.emptyList();
    }
    final Charset charset = contentType.getCharset(DEFAULT_CHARSET);
    final CharArrayBuffer buf;
    try (final InputStream inStream = entity.getContent()) {
        if (inStream == null) {
            return Collections.emptyList();
        }
        buf = toCharArrayBuffer(inStream, contentLength, charset, maxStreamLength);
    }
    if (buf.isEmpty()) {
        return Collections.emptyList();
    }
    return WWWFormCodec.parse(buf, charset);
}
Also used : ContentType(org.apache.hc.core5.http.ContentType) InputStream(java.io.InputStream) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Charset(java.nio.charset.Charset)

Example 24 with NameValuePair

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

the class BasicHeaderElement method toString.

@Override
public String toString() {
    final StringBuilder buffer = new StringBuilder();
    buffer.append(this.name);
    if (this.value != null) {
        buffer.append("=");
        buffer.append(this.value);
    }
    for (final NameValuePair parameter : this.parameters) {
        buffer.append("; ");
        buffer.append(parameter);
    }
    return buffer.toString();
}
Also used : NameValuePair(org.apache.hc.core5.http.NameValuePair)

Example 25 with NameValuePair

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

the class BasicHeaderElement method getParameterByName.

@Override
public NameValuePair getParameterByName(final String name) {
    Args.notNull(name, "Name");
    NameValuePair found = null;
    for (final NameValuePair current : this.parameters) {
        if (current.getName().equalsIgnoreCase(name)) {
            found = current;
            break;
        }
    }
    return found;
}
Also used : NameValuePair(org.apache.hc.core5.http.NameValuePair)

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