use of com.helger.commons.codec.URLCodec in project ph-web by phax.
the class HttpClientHelper method createParameterEntity.
@Nullable
public static HttpEntity createParameterEntity(@Nullable final Map<String, String> aMap, @Nonnull final Charset aCharset) {
ValueEnforcer.notNull(aCharset, "Charset");
if (aMap == null || aMap.isEmpty())
return null;
try (final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream(1024)) {
final URLCodec aURLCodec = new URLCodec();
boolean bFirst = true;
for (final Map.Entry<String, String> aEntry : aMap.entrySet()) {
if (bFirst)
bFirst = false;
else
aBAOS.write('&');
// Key must be present
final String sKey = aEntry.getKey();
aURLCodec.encode(sKey.getBytes(aCharset), aBAOS);
// Value is optional
final String sValue = aEntry.getValue();
if (StringHelper.hasText(sValue)) {
aBAOS.write('=');
aURLCodec.encode(sValue.getBytes(aCharset), aBAOS);
}
}
return new InputStreamEntity(aBAOS.getAsInputStream());
}
}
Aggregations