本文整理汇总了Java中org.apache.http.protocol.HTTP.DEF_CONTENT_CHARSET属性的典型用法代码示例。如果您正苦于以下问题:Java HTTP.DEF_CONTENT_CHARSET属性的具体用法?Java HTTP.DEF_CONTENT_CHARSET怎么用?Java HTTP.DEF_CONTENT_CHARSET使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类org.apache.http.protocol.HTTP
的用法示例。
在下文中一共展示了HTTP.DEF_CONTENT_CHARSET属性的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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.
*
* @param entity
* The entity to parse
* @throws IOException
* If there was an exception getting the entity's data.
*/
public static List <NameValuePair> parse (
final HttpEntity entity) throws IOException {
ContentType contentType = ContentType.get(entity);
if (contentType != null && contentType.getMimeType().equalsIgnoreCase(CONTENT_TYPE)) {
String content = EntityUtils.toString(entity, Consts.ASCII);
if (content != null && content.length() > 0) {
Charset charset = contentType.getCharset();
if (charset == null) {
charset = HTTP.DEF_CONTENT_CHARSET;
}
return parse(content, charset);
}
}
return Collections.emptyList();
}
示例2: StringEntity
/**
* Creates a StringEntity with the specified content and content type.
*
* @param string content to be used. Not {@code null}.
* @param contentType content type to be used. May be {@code null}, in which case the default
* MIME type {@link ContentType#TEXT_PLAIN} is assumed.
*
* @throws IllegalArgumentException if the string parameter is null
*
* @since 4.2
*/
public StringEntity(final String string, final ContentType contentType) {
super();
if (string == null) {
throw new IllegalArgumentException("Source string may not be null");
}
Charset charset = contentType != null ? contentType.getCharset() : null;
if (charset == null) {
charset = HTTP.DEF_CONTENT_CHARSET;
}
try {
this.content = string.getBytes(charset.name());
} catch (UnsupportedEncodingException ex) {
// should never happen
throw new UnsupportedCharsetException(charset.name());
}
if (contentType != null) {
setContentType(contentType.toString());
}
}
示例3: setContent
public void setContent(final String source, final ContentType contentType) throws UnsupportedCharsetException {
Args.notNull(source, "Source string");
Charset charset = contentType != null?contentType.getCharset():null;
if(charset == null) {
charset = HTTP.DEF_CONTENT_CHARSET;
}
try {
this.content = new BytesArray(source.getBytes(charset.name()));
} catch (UnsupportedEncodingException var) {
throw new UnsupportedCharsetException(charset.name());
}
if(contentType != null) {
addHeader("Content-Type", contentType.toString());
}
}
示例4: toString
/**
* Get the entity content as a String, using the provided default character set
* if none is found in the entity.
* If defaultCharset is null, the default "ISO-8859-1" is used.
*
* @param entity must not be null
* @param defaultCharset character set to be applied if none found in the entity
* @return the entity content as a String. May be null if
* {@link HttpEntity#getContent()} is null.
* @throws ParseException if header elements cannot be parsed
* @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE
* @throws IOException if an error occurs reading the input stream
*/
public static String toString(
final HttpEntity entity, final Charset defaultCharset) throws IOException, ParseException {
if (entity == null) {
throw new IllegalArgumentException("HTTP entity may not be null");
}
InputStream instream = entity.getContent();
if (instream == null) {
return null;
}
try {
if (entity.getContentLength() > Integer.MAX_VALUE) {
throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
}
int i = (int)entity.getContentLength();
if (i < 0) {
i = 4096;
}
ContentType contentType = ContentType.getOrDefault(entity);
Charset charset = contentType.getCharset();
if (charset == null) {
charset = defaultCharset;
}
if (charset == null) {
charset = HTTP.DEF_CONTENT_CHARSET;
}
Reader reader = new InputStreamReader(instream, charset);
CharArrayBuffer buffer = new CharArrayBuffer(i);
char[] tmp = new char[1024];
int l;
while((l = reader.read(tmp)) != -1) {
buffer.append(tmp, 0, l);
}
return buffer.toString();
} finally {
instream.close();
}
}
示例5: handleResponse
@Override
public AuthenticationResponse handleResponse(final HttpResponse response) throws IOException {
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
Charset charset = HTTP.DEF_CONTENT_CHARSET;
ContentType contentType = ContentType.get(response.getEntity());
if(contentType != null) {
if(contentType.getCharset() != null) {
charset = contentType.getCharset();
}
}
try {
final JsonParser parser = new JsonParser();
final JsonObject json = parser.parse(new InputStreamReader(response.getEntity().getContent(), charset)).getAsJsonObject();
final String token = json.getAsJsonPrimitive("token").getAsString();
final String endpoint = json.getAsJsonPrimitive("endpoint").getAsString();
return new AuthenticationResponse(response, token,
Collections.singleton(new Region(null, URI.create(endpoint), null, true)));
}
catch(JsonParseException e) {
throw new IOException(e.getMessage(), e);
}
}
else if(response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED
|| response.getStatusLine().getStatusCode() == HttpStatus.SC_FORBIDDEN) {
throw new AuthorizationException(new Response(response));
}
throw new GenericException(new Response(response));
}