本文整理汇总了Java中org.apache.http.HeaderElement类的典型用法代码示例。如果您正苦于以下问题:Java HeaderElement类的具体用法?Java HeaderElement怎么用?Java HeaderElement使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HeaderElement类属于org.apache.http包,在下文中一共展示了HeaderElement类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getFileNameFromHttpResponse
import org.apache.http.HeaderElement; //导入依赖的package包/类
public static String getFileNameFromHttpResponse(final HttpResponse response) {
if (response == null) return null;
String result = null;
Header header = response.getFirstHeader("Content-Disposition");
if (header != null) {
for (HeaderElement element : header.getElements()) {
NameValuePair fileNamePair = element.getParameterByName("filename");
if (fileNamePair != null) {
result = fileNamePair.getValue();
// try to get correct encoding str
result = CharsetUtils.toCharset(result, HTTP.UTF_8, result.length());
break;
}
}
}
return result;
}
示例2: getCharsetFromHttpResponse
import org.apache.http.HeaderElement; //导入依赖的package包/类
public static String getCharsetFromHttpResponse(final HttpResponse response) {
if (response == null) return null;
String result = null;
Header header = response.getEntity().getContentType();
if (header != null) {
for (HeaderElement element : header.getElements()) {
NameValuePair charsetPair = element.getParameterByName("charset");
if (charsetPair != null) {
result = charsetPair.getValue();
break;
}
}
}
boolean isSupportedCharset = false;
if (!TextUtils.isEmpty(result)) {
try {
isSupportedCharset = Charset.isSupported(result);
} catch (Throwable e) {
}
}
return isSupportedCharset ? result : null;
}
示例3: parseHeader
import org.apache.http.HeaderElement; //导入依赖的package包/类
public HeaderElement parseHeader(
final CharArrayBuffer buffer,
final ParserCursor cursor) throws ParseException {
if (buffer == null) {
throw new IllegalArgumentException("Char array buffer may not be null");
}
if (cursor == null) {
throw new IllegalArgumentException("Parser cursor may not be null");
}
NameValuePair nvp = parseNameValuePair(buffer, cursor);
List<NameValuePair> params = new ArrayList<NameValuePair>();
while (!cursor.atEnd()) {
NameValuePair param = parseNameValuePair(buffer, cursor);
params.add(param);
}
return new BasicHeaderElement(
nvp.getName(),
nvp.getValue(), params.toArray(new NameValuePair[params.size()]));
}
示例4: getKeepAliveDuration
import org.apache.http.HeaderElement; //导入依赖的package包/类
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
if (response == null) {
throw new IllegalArgumentException("HTTP response may not be null");
}
HeaderElementIterator it = new BasicHeaderElementIterator(
response.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
HeaderElement he = it.nextElement();
String param = he.getName();
String value = he.getValue();
if (value != null && param.equalsIgnoreCase("timeout")) {
try {
return Long.parseLong(value) * 1000;
} catch(NumberFormatException ignore) {
}
}
}
return -1;
}
示例5: getContentCharSet
import org.apache.http.HeaderElement; //导入依赖的package包/类
/**
* Obtains character set of the entity, if known.
*
* @param entity must not be null
* @return the character set, or null if not found
* @throws ParseException if header elements cannot be parsed
* @throws IllegalArgumentException if entity is null
*
* @deprecated (4.1.3) use {@link ContentType#getOrDefault(HttpEntity)}
*/
@Deprecated
public static String getContentCharSet(final HttpEntity entity) throws ParseException {
if (entity == null) {
throw new IllegalArgumentException("HTTP entity may not be null");
}
String charset = null;
if (entity.getContentType() != null) {
HeaderElement values[] = entity.getContentType().getElements();
if (values.length > 0) {
NameValuePair param = values[0].getParameterByName("charset");
if (param != null) {
charset = param.getValue();
}
}
}
return charset;
}
示例6: getAllowedMethods
import org.apache.http.HeaderElement; //导入依赖的package包/类
public Set<String> getAllowedMethods(final HttpResponse response) {
if (response == null) {
throw new IllegalArgumentException("HTTP response may not be null");
}
HeaderIterator it = response.headerIterator("Allow");
Set<String> methods = new HashSet<String>();
while (it.hasNext()) {
Header header = it.nextHeader();
HeaderElement[] elements = header.getElements();
for (HeaderElement element : elements) {
methods.add(element.getName());
}
}
return methods;
}
示例7: parse
import org.apache.http.HeaderElement; //导入依赖的package包/类
@Override
public List<Cookie> parse(
final Header header,
CookieOrigin origin) throws MalformedCookieException {
if (header == null) {
throw new IllegalArgumentException("Header may not be null");
}
if (origin == null) {
throw new IllegalArgumentException("Cookie origin may not be null");
}
if (!header.getName().equalsIgnoreCase(SM.SET_COOKIE2)) {
throw new MalformedCookieException("Unrecognized cookie header '"
+ header.toString() + "'");
}
origin = adjustEffectiveHost(origin);
HeaderElement[] elems = header.getElements();
return createCookies(elems, origin);
}
示例8: create
import org.apache.http.HeaderElement; //导入依赖的package包/类
private static ContentType create(final HeaderElement helem) {
String mimeType = helem.getName();
String charset = null;
NameValuePair param = helem.getParameterByName("charset");
if (param != null) {
charset = param.getValue();
}
return create(mimeType, charset);
}
示例9: estimateHeaderElementLen
import org.apache.http.HeaderElement; //导入依赖的package包/类
/**
* Estimates the length of a formatted header element.
*
* @param elem the header element to format, or <code>null</code>
*
* @return a length estimate, in number of characters
*/
protected int estimateHeaderElementLen(final HeaderElement elem) {
if (elem == null)
return 0;
int result = elem.getName().length(); // name
final String value = elem.getValue();
if (value != null) {
// assume quotes, but no escaped characters
result += 3 + value.length(); // ="value"
}
final int parcnt = elem.getParameterCount();
if (parcnt > 0) {
for (int i=0; i<parcnt; i++) {
result += 2 + // ; <param>
estimateNameValuePairLen(elem.getParameter(i));
}
}
return result;
}
示例10: parseElements
import org.apache.http.HeaderElement; //导入依赖的package包/类
/**
* Parses elements with the given parser.
*
* @param value the header value to parse
* @param parser the parser to use, or <code>null</code> for default
*
* @return array holding the header elements, never <code>null</code>
*/
public final static
HeaderElement[] parseElements(final String value,
HeaderValueParser parser)
throws ParseException {
if (value == null) {
throw new IllegalArgumentException
("Value to parse may not be null");
}
if (parser == null)
parser = BasicHeaderValueParser.DEFAULT;
CharArrayBuffer buffer = new CharArrayBuffer(value.length());
buffer.append(value);
ParserCursor cursor = new ParserCursor(0, value.length());
return parser.parseElements(buffer, cursor);
}
示例11: parseHeaderElement
import org.apache.http.HeaderElement; //导入依赖的package包/类
/**
* Parses an element with the given parser.
*
* @param value the header element to parse
* @param parser the parser to use, or <code>null</code> for default
*
* @return the parsed header element
*/
public final static
HeaderElement parseHeaderElement(final String value,
HeaderValueParser parser)
throws ParseException {
if (value == null) {
throw new IllegalArgumentException
("Value to parse may not be null");
}
if (parser == null)
parser = BasicHeaderValueParser.DEFAULT;
CharArrayBuffer buffer = new CharArrayBuffer(value.length());
buffer.append(value);
ParserCursor cursor = new ParserCursor(0, value.length());
return parser.parseHeaderElement(buffer, cursor);
}
示例12: getContentTypeCharset
import org.apache.http.HeaderElement; //导入依赖的package包/类
public String getContentTypeCharset(String defaultCharset) {
List<String> values = getHeader(Http.Headers.CONTENT_TYPE);
if (values!=null) {
for (String value: values) {
HeaderElement[] headerElements = BasicHeaderValueParser.parseElements(value, (HeaderValueParser) null);
if (headerElements!=null && headerElements.length>0) {
NameValuePair charsetPair = headerElements[0].getParameterByName("charset");
if (charsetPair!=null) {
return charsetPair.getValue();
}
}
}
}
return defaultCharset;
}
示例13: getKeepAliveDuration
import org.apache.http.HeaderElement; //导入依赖的package包/类
@Override
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
HeaderElement he = it.nextElement();
String param = he.getName();
String value = he.getValue();
if (value != null && param.equalsIgnoreCase("timeout")) {
long timeout = Long.parseLong(value) * 1000;
if (timeout > 20 * 1000) {
return 20 * 1000;
} else {
return timeout;
}
}
}
return 5 * 1000;
}
示例14: getCharsetFromHttpRequest
import org.apache.http.HeaderElement; //导入依赖的package包/类
public static Charset getCharsetFromHttpRequest(final HttpRequestBase request) {
if (request == null) return null;
String charsetName = null;
Header header = request.getFirstHeader("Content-Type");
if (header != null) {
for (HeaderElement element : header.getElements()) {
NameValuePair charsetPair = element.getParameterByName("charset");
if (charsetPair != null) {
charsetName = charsetPair.getValue();
break;
}
}
}
boolean isSupportedCharset = false;
if (!TextUtils.isEmpty(charsetName)) {
try {
isSupportedCharset = Charset.isSupported(charsetName);
} catch (Throwable e) {
}
}
return isSupportedCharset ? Charset.forName(charsetName) : null;
}
示例15: getKeepAliveDuration
import org.apache.http.HeaderElement; //导入依赖的package包/类
@Override
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
// Honor 'keep-alive' header
HeaderElementIterator it = new BasicHeaderElementIterator(
response.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
HeaderElement he = it.nextElement();
String param = he.getName();
String value = he.getValue();
if (value != null && param.equalsIgnoreCase("timeout")) {
try {
return Long.parseLong(value) * 1000;
} catch (NumberFormatException ignore) {
// Do nothing
}
}
}
// otherwise keep alive for 30 seconds
return 30 * 1000;
}