當前位置: 首頁>>代碼示例>>Java>>正文


Java ContentType.get方法代碼示例

本文整理匯總了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();
}
 
開發者ID:reportportal,項目名稱:client-java-httpclient-repacked,代碼行數:28,代碼來源:URLEncodedUtils.java

示例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());
}
 
開發者ID:reportportal,項目名稱:client-java-httpclient-repacked,代碼行數:10,代碼來源:TestContentType.java

示例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());
}
 
開發者ID:reportportal,項目名稱:client-java-httpclient-repacked,代碼行數:10,代碼來源:TestContentType.java

示例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();
    }
}
 
開發者ID:reportportal,項目名稱:client-java-httpclient-repacked,代碼行數:57,代碼來源:EntityUtils.java


注:本文中的com.epam.reportportal.apache.http.entity.ContentType.get方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。