本文整理汇总了Java中org.apache.http.entity.ContentType.get方法的典型用法代码示例。如果您正苦于以下问题:Java ContentType.get方法的具体用法?Java ContentType.get怎么用?Java ContentType.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.entity.ContentType
的用法示例。
在下文中一共展示了ContentType.get方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: ensureJsonContent
import org.apache.http.entity.ContentType; //导入方法依赖的package包/类
private static ContentType ensureJsonContent(HttpEntity entity)
throws ClientProtocolException {
ContentType type = ContentType.get(entity);
String expected = ContentType.APPLICATION_JSON // (1)
.toString()
.replace(" ", ""); // (2)
if (type == null) {
throw new ClientProtocolException("Unspecified content type.");
}
String actual = type.toString().replace(" ", ""); // (2)
if (!expected.equalsIgnoreCase(actual)) {
throw new ClientProtocolException(
String.format("Invalid content type: %s", actual));
}
return type;
}
示例2: parse
import org.apache.http.entity.ContentType; //导入方法依赖的package包/类
/**
* 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();
}
示例3: handleResponse
import org.apache.http.entity.ContentType; //导入方法依赖的package包/类
@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));
}
示例4: setResponse
import org.apache.http.entity.ContentType; //导入方法依赖的package包/类
public void setResponse(CloseableHttpResponse response) throws UnsupportedOperationException, IOException {
this.response = response;
if(response.getEntity()!=null) {
byte[] byteArray = IOUtils.toByteArray(response.getEntity().getContent());
ContentType contentType = ContentType.get(response.getEntity());
response.close();
this.content=new Content(byteArray, contentType);
}
}
示例5: getCharset
import org.apache.http.entity.ContentType; //导入方法依赖的package包/类
/**
* 获取响应编码,如果是文本的话
*
*
* @date 2015年7月18日
* @return
*/
public Charset getCharset() {
ContentType contentType = ContentType.get(entity);
if (contentType == null)
return null;
return contentType.getCharset();
}