diff --git a/httpclient/src/main/java/org/apache/http/client/entity/UrlEncodedFormEntity.java b/httpclient/src/main/java/org/apache/http/client/entity/UrlEncodedFormEntity.java index 2a1e333a0..37705f441 100644 --- a/httpclient/src/main/java/org/apache/http/client/entity/UrlEncodedFormEntity.java +++ b/httpclient/src/main/java/org/apache/http/client/entity/UrlEncodedFormEntity.java @@ -33,6 +33,7 @@ import org.apache.http.annotation.NotThreadSafe; import org.apache.http.NameValuePair; import org.apache.http.client.utils.URLEncodedUtils; +import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.protocol.HTTP; @@ -45,20 +46,38 @@ import org.apache.http.protocol.HTTP; @NotThreadSafe // AbstractHttpEntity is not thread-safe public class UrlEncodedFormEntity extends StringEntity { + public static UrlEncodedFormEntity create( + final Iterable parameters, final String charset) { + try { + return new UrlEncodedFormEntity(parameters, charset); + } catch (UnsupportedEncodingException ex) { + throw new IllegalArgumentException(ex.getMessage(), ex); + } + } + + public static UrlEncodedFormEntity create( + final Iterable parameters) { + try { + return new UrlEncodedFormEntity(parameters, null); + } catch (UnsupportedEncodingException ex) { + throw new IllegalArgumentException(ex.getMessage(), ex); + } + } + /** * Constructs a new {@link UrlEncodedFormEntity} with the list * of parameters in the specified encoding. * * @param parameters list of name/value pairs - * @param encoding encoding the name/value pairs be encoded with + * @param charset encoding the name/value pairs be encoded with * @throws UnsupportedEncodingException if the encoding isn't supported */ public UrlEncodedFormEntity ( final List parameters, - final String encoding) throws UnsupportedEncodingException { - super(URLEncodedUtils.format(parameters, encoding), encoding); - setContentType(URLEncodedUtils.CONTENT_TYPE + HTTP.CHARSET_PARAM + - (encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET)); + final String charset) throws UnsupportedEncodingException { + super(URLEncodedUtils.format(parameters, + charset != null ? charset : HTTP.DEFAULT_CONTENT_CHARSET), + ContentType.create(URLEncodedUtils.CONTENT_TYPE, charset)); } /** @@ -66,17 +85,17 @@ public class UrlEncodedFormEntity extends StringEntity { * of parameters in the specified encoding. * * @param parameters iterable collection of name/value pairs - * @param encoding encoding the name/value pairs be encoded with + * @param charset encoding the name/value pairs be encoded with * @throws UnsupportedEncodingException if the encoding isn't supported * * @since 4.2 */ public UrlEncodedFormEntity ( final Iterable parameters, - final String encoding) throws UnsupportedEncodingException { - super(URLEncodedUtils.format(parameters, encoding), encoding); - setContentType(URLEncodedUtils.CONTENT_TYPE + HTTP.CHARSET_PARAM + - (encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET)); + final String charset) throws UnsupportedEncodingException { + super(URLEncodedUtils.format(parameters, + charset != null ? charset : HTTP.DEFAULT_CONTENT_CHARSET), + ContentType.create(URLEncodedUtils.CONTENT_TYPE, charset)); } /** @@ -88,7 +107,7 @@ public class UrlEncodedFormEntity extends StringEntity { */ public UrlEncodedFormEntity ( final List parameters) throws UnsupportedEncodingException { - this(parameters, HTTP.DEFAULT_CONTENT_CHARSET); + this(parameters, null); } /** @@ -102,7 +121,7 @@ public class UrlEncodedFormEntity extends StringEntity { */ public UrlEncodedFormEntity ( final Iterable parameters) throws UnsupportedEncodingException { - this(parameters, HTTP.DEFAULT_CONTENT_CHARSET); + this(parameters, null); } } diff --git a/httpclient/src/main/java/org/apache/http/client/utils/URLEncodedUtils.java b/httpclient/src/main/java/org/apache/http/client/utils/URLEncodedUtils.java index 724003786..aa18236d1 100644 --- a/httpclient/src/main/java/org/apache/http/client/utils/URLEncodedUtils.java +++ b/httpclient/src/main/java/org/apache/http/client/utils/URLEncodedUtils.java @@ -244,17 +244,17 @@ public class URLEncodedUtils { * list of parameters in an HTTP PUT or HTTP POST. * * @param parameters The parameters to include. - * @param encoding The encoding to use. + * @param charset The encoding to use. * * @since 4.2 */ public static String format ( final Iterable parameters, - final String encoding) { + final String charset) { final StringBuilder result = new StringBuilder(); for (final NameValuePair parameter : parameters) { - final String encodedName = encode(parameter.getName(), encoding); - final String encodedValue = encode(parameter.getValue(), encoding); + final String encodedName = encode(parameter.getName(), charset); + final String encodedValue = encode(parameter.getValue(), charset); if (result.length() > 0) { result.append(PARAMETER_SEPARATOR); } @@ -267,25 +267,25 @@ public class URLEncodedUtils { return result.toString(); } - private static String decode (final String content, final String encoding) { + private static String decode (final String content, final String charset) { if (content == null) { return null; } try { return URLDecoder.decode(content, - encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET); + charset != null ? charset : HTTP.DEFAULT_CONTENT_CHARSET); } catch (UnsupportedEncodingException problem) { throw new IllegalArgumentException(problem); } } - private static String encode (final String content, final String encoding) { + private static String encode (final String content, final String charset) { if (content == null) { return null; } try { return URLEncoder.encode(content, - encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET); + charset != null ? charset : HTTP.DEFAULT_CONTENT_CHARSET); } catch (UnsupportedEncodingException problem) { throw new IllegalArgumentException(problem); }