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);
}
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);
}
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);
}
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();
}
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;
}
Aggregations