public StringRequestEntity(String content,
String contentType,
String charset) throws UnsupportedEncodingException {
super();
if (content == null) {
throw new IllegalArgumentException("The content cannot be null");
}
this.contentType = contentType;
this.charset = charset;
// resolve the content type and the charset
if (contentType != null) {
HeaderElement[] values = HeaderElement.parseElements(contentType);
NameValuePair charsetPair = null;
for (int i = 0; i < values.length; i++) {
if ((charsetPair = values[i].getParameterByName("charset")) != null) {
// charset found
break;
}
}
if (charset == null && charsetPair != null) {
// use the charset from the content type
this.charset = charsetPair.getValue();
} else if (charset != null && charsetPair == null) {
// append the charset to the content type
this.contentType = contentType + "; charset=" + charset;
}
}
if (this.charset != null) {
this.content = content.getBytes(this.charset);
} else {
this.content = content.getBytes();
}
}
Creates a new entity with the given content, content type, and charset. Parameters:
content - The content to set.
contentType - The type of the content, or null. The value retured
by #getContentType() . If this content type contains a charset and the charset
parameter is null, the content's type charset will be used.
charset - The charset of the content, or null. Used to convert the
content to bytes. If the content type does not contain a charset and charset is not null,
then the charset will be appended to the content type.
|