本文整理汇总了Java中com.epam.reportportal.apache.http.entity.ContentType.get方法的典型用法代码示例。如果您正苦于以下问题:Java ContentType.get方法的具体用法?Java ContentType.get怎么用?Java ContentType.get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.epam.reportportal.apache.http.entity.ContentType
的用法示例。
在下文中一共展示了ContentType.get方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: parse
import com.epam.reportportal.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
* @return a list of {@link NameValuePair} as built from the URI's query portion.
* @throws IOException
* If there was an exception getting the entity's data.
*/
public static List <NameValuePair> parse(
final HttpEntity entity) throws IOException {
final ContentType contentType = ContentType.get(entity);
if (contentType != null && contentType.getMimeType().equalsIgnoreCase(CONTENT_TYPE)) {
final 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, QP_SEPS);
}
}
return Collections.emptyList();
}
示例2: testExtract
import com.epam.reportportal.apache.http.entity.ContentType; //导入方法依赖的package包/类
@Test
public void testExtract() throws Exception {
final BasicHttpEntity httpentity = new BasicHttpEntity();
httpentity.setContentType(new BasicHeader("Content-Type", "text/plain; charset = UTF-8"));
final ContentType contentType = ContentType.get(httpentity);
Assert.assertNotNull(contentType);
Assert.assertEquals("text/plain", contentType.getMimeType());
Assert.assertEquals("UTF-8", contentType.getCharset().name());
}
示例3: testExtractNoCharset
import com.epam.reportportal.apache.http.entity.ContentType; //导入方法依赖的package包/类
@Test
public void testExtractNoCharset() throws Exception {
final BasicHttpEntity httpentity = new BasicHttpEntity();
httpentity.setContentType(new BasicHeader("Content-Type", "text/plain; param=yadayada"));
final ContentType contentType = ContentType.get(httpentity);
Assert.assertNotNull(contentType);
Assert.assertEquals("text/plain", contentType.getMimeType());
Assert.assertNull(contentType.getCharset());
}
示例4: toString
import com.epam.reportportal.apache.http.entity.ContentType; //导入方法依赖的package包/类
/**
* 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
* @throws UnsupportedCharsetException Thrown when the named charset is not available in
* this instance of the Java virtual machine
*/
public static String toString(
final HttpEntity entity, final Charset defaultCharset) throws IOException, ParseException {
Args.notNull(entity, "Entity");
final InputStream instream = entity.getContent();
if (instream == null) {
return null;
}
try {
Args.check(entity.getContentLength() <= Integer.MAX_VALUE,
"HTTP entity too large to be buffered in memory");
int i = (int)entity.getContentLength();
if (i < 0) {
i = 4096;
}
Charset charset = null;
try {
final ContentType contentType = ContentType.get(entity);
if (contentType != null) {
charset = contentType.getCharset();
}
} catch (final UnsupportedCharsetException ex) {
throw new UnsupportedEncodingException(ex.getMessage());
}
if (charset == null) {
charset = defaultCharset;
}
if (charset == null) {
charset = HTTP.DEF_CONTENT_CHARSET;
}
final Reader reader = new InputStreamReader(instream, charset);
final CharArrayBuffer buffer = new CharArrayBuffer(i);
final char[] tmp = new char[1024];
int l;
while((l = reader.read(tmp)) != -1) {
buffer.append(tmp, 0, l);
}
return buffer.toString();
} finally {
instream.close();
}
}