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


Java ContentType.getCharset方法代碼示例

本文整理匯總了Java中org.apache.http.entity.ContentType.getCharset方法的典型用法代碼示例。如果您正苦於以下問題:Java ContentType.getCharset方法的具體用法?Java ContentType.getCharset怎麽用?Java ContentType.getCharset使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.http.entity.ContentType的用法示例。


在下文中一共展示了ContentType.getCharset方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: 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();
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:28,代碼來源:URLEncodedUtils.java

示例2: setContent

import org.apache.http.entity.ContentType; //導入方法依賴的package包/類
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());
    }
}
 
開發者ID:baidu,項目名稱:Elasticsearch,代碼行數:18,代碼來源:LocalRestRequest.java

示例3: fromEntity

import org.apache.http.entity.ContentType; //導入方法依賴的package包/類
static <T> T fromEntity(HttpEntity resource, Class<T> resourceType)
        throws IOException {
    ContentType type = ensureJsonContent(resource);
    Reader source = new InputStreamReader(
                            resource.getContent(), type.getCharset());
    return new JsonSourceReader<>(resourceType, source).read();
}
 
開發者ID:openmicroscopy,項目名稱:omero-ms-queue,代碼行數:8,代碼來源:JsonEntity.java

示例4: toString

import org.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
 */
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();
    }
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:51,代碼來源:EntityUtils.java

示例5: 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));
}
 
開發者ID:iterate-ch,項目名稱:cyberduck,代碼行數:29,代碼來源:HubicAuthenticationResponseHandler.java

示例6: getCharset

import org.apache.http.entity.ContentType; //導入方法依賴的package包/類
public static String getCharset(HttpResponse response) {
  ContentType contentType = ContentType.getOrDefault(response.getEntity());
  Charset charset = contentType.getCharset();
  return charset == null ? "UTF-8" : charset.name();
}
 
開發者ID:EHRI,項目名稱:rs-aggregator,代碼行數:6,代碼來源:AbstractUriReader.java

示例7: getResponseCharset

import org.apache.http.entity.ContentType; //導入方法依賴的package包/類
public static String getResponseCharset(HttpResponse resp) {
	ContentType ctype = ContentType.getOrDefault(resp.getEntity());
	if (ctype.getCharset() != null)
		return ctype.getCharset().name();
	return null;
}
 
開發者ID:ichatter,項目名稱:dcits-report,代碼行數:7,代碼來源:HttpHeaderUtil.java

示例8: 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();
}
 
開發者ID:swxiao,項目名稱:bubble2,代碼行數:14,代碼來源:ResponseWrap.java


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