本文整理汇总了Java中ch.boye.httpclientandroidlib.protocol.HTTP类的典型用法代码示例。如果您正苦于以下问题:Java HTTP类的具体用法?Java HTTP怎么用?Java HTTP使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HTTP类属于ch.boye.httpclientandroidlib.protocol包,在下文中一共展示了HTTP类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: StringEntity
import ch.boye.httpclientandroidlib.protocol.HTTP; //导入依赖的package包/类
/**
* Creates a StringEntity with the specified content and content type.
*
* @param string content to be used. Not {@code null}.
* @param contentType content type to be used. May be {@code null}, in which case the default
* MIME type {@link ContentType#TEXT_PLAIN} is assumed.
*
* @throws IllegalArgumentException if the string parameter is null
* @throws UnsupportedCharsetException Thrown when the named charset is not available in
* this instance of the Java virtual machine
* @since 4.2
*/
public StringEntity(final String string, final ContentType contentType) throws UnsupportedCharsetException {
super();
Args.notNull(string, "Source string");
Charset charset = contentType != null ? contentType.getCharset() : null;
if (charset == null) {
charset = HTTP.DEF_CONTENT_CHARSET;
}
try {
this.content = string.getBytes(charset.name());
} catch (final UnsupportedEncodingException ex) {
// should never happen
throw new UnsupportedCharsetException(charset.name());
}
if (contentType != null) {
setContentType(contentType.toString());
}
}
示例2: process
import ch.boye.httpclientandroidlib.protocol.HTTP; //导入依赖的package包/类
public void process(final HttpRequest request, final HttpContext context)
throws HttpException, IOException {
Args.notNull(request, "HTTP request");
if (!request.containsHeader(HTTP.EXPECT_DIRECTIVE)) {
if (request instanceof HttpEntityEnclosingRequest) {
final ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
final HttpEntity entity = ((HttpEntityEnclosingRequest)request).getEntity();
// Do not send the expect header if request body is known to be empty
if (entity != null
&& entity.getContentLength() != 0 && !ver.lessEquals(HttpVersion.HTTP_1_0)) {
final HttpClientContext clientContext = HttpClientContext.adapt(context);
final RequestConfig config = clientContext.getRequestConfig();
if (config.isExpectContinueEnabled()) {
request.addHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE);
}
}
}
}
}
示例3: parse
import ch.boye.httpclientandroidlib.protocol.HTTP; //导入依赖的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();
}
示例4: substringTrimmed
import ch.boye.httpclientandroidlib.protocol.HTTP; //导入依赖的package包/类
/**
* Returns a substring of this buffer with leading and trailing whitespace
* omitted. The substring begins with the first non-whitespace character
* from <code>beginIndex</code> and extends to the last
* non-whitespace character with the index lesser than
* <code>endIndex</code>.
*
* @param from the beginning index, inclusive.
* @param to the ending index, exclusive.
* @return the specified substring.
* @exception IndexOutOfBoundsException if the
* <code>beginIndex</code> is negative, or
* <code>endIndex</code> is larger than the length of this
* buffer, or <code>beginIndex</code> is larger than
* <code>endIndex</code>.
*/
public String substringTrimmed(final int from, final int to) {
int beginIndex = from;
int endIndex = to;
if (beginIndex < 0) {
throw new IndexOutOfBoundsException("Negative beginIndex: "+beginIndex);
}
if (endIndex > this.len) {
throw new IndexOutOfBoundsException("endIndex: "+endIndex+" > length: "+this.len);
}
if (beginIndex > endIndex) {
throw new IndexOutOfBoundsException("beginIndex: "+beginIndex+" > endIndex: "+endIndex);
}
while (beginIndex < endIndex && HTTP.isWhitespace(this.buffer[beginIndex])) {
beginIndex++;
}
while (endIndex > beginIndex && HTTP.isWhitespace(this.buffer[endIndex - 1])) {
endIndex--;
}
return new String(this.buffer, beginIndex, endIndex - beginIndex);
}
示例5: getKeepAliveDuration
import ch.boye.httpclientandroidlib.protocol.HTTP; //导入依赖的package包/类
public long getKeepAliveDuration(final HttpResponse response, final HttpContext context) {
Args.notNull(response, "HTTP response");
final HeaderElementIterator it = new BasicHeaderElementIterator(
response.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
final HeaderElement he = it.nextElement();
final String param = he.getName();
final String value = he.getValue();
if (value != null && param.equalsIgnoreCase("timeout")) {
try {
return Long.parseLong(value) * 1000;
} catch(final NumberFormatException ignore) {
}
}
}
return -1;
}
示例6: revalidationResponseIsTooOld
import ch.boye.httpclientandroidlib.protocol.HTTP; //导入依赖的package包/类
private boolean revalidationResponseIsTooOld(final HttpResponse backendResponse,
final HttpCacheEntry cacheEntry) {
final Header entryDateHeader = cacheEntry.getFirstHeader(HTTP.DATE_HEADER);
final Header responseDateHeader = backendResponse.getFirstHeader(HTTP.DATE_HEADER);
if (entryDateHeader != null && responseDateHeader != null) {
final Date entryDate = DateUtils.parseDate(entryDateHeader.getValue());
final Date respDate = DateUtils.parseDate(responseDateHeader.getValue());
if (entryDate == null || respDate == null) {
// either backend response or cached entry did not have a valid
// Date header, so we can't tell if they are out of order
// according to the origin clock; thus we can skip the
// unconditional retry recommended in 13.2.6 of RFC 2616.
return false;
}
if (respDate.before(entryDate)) {
return true;
}
}
return false;
}
示例7: alreadyHaveNewerCacheEntry
import ch.boye.httpclientandroidlib.protocol.HTTP; //导入依赖的package包/类
private boolean alreadyHaveNewerCacheEntry(final HttpHost target, final HttpRequestWrapper request,
final HttpResponse backendResponse) {
HttpCacheEntry existing = null;
try {
existing = responseCache.getCacheEntry(target, request);
} catch (final IOException ioe) {
// nop
}
if (existing == null) {
return false;
}
final Header entryDateHeader = existing.getFirstHeader(HTTP.DATE_HEADER);
if (entryDateHeader == null) {
return false;
}
final Header responseDateHeader = backendResponse.getFirstHeader(HTTP.DATE_HEADER);
if (responseDateHeader == null) {
return false;
}
final Date entryDate = DateUtils.parseDate(entryDateHeader.getValue());
final Date responseDate = DateUtils.parseDate(responseDateHeader.getValue());
if (entryDate == null || responseDate == null) {
return false;
}
return responseDate.before(entryDate);
}
示例8: isIncompleteResponse
import ch.boye.httpclientandroidlib.protocol.HTTP; //导入依赖的package包/类
boolean isIncompleteResponse(final HttpResponse resp, final Resource resource) {
final int status = resp.getStatusLine().getStatusCode();
if (status != HttpStatus.SC_OK
&& status != HttpStatus.SC_PARTIAL_CONTENT) {
return false;
}
final Header hdr = resp.getFirstHeader(HTTP.CONTENT_LEN);
if (hdr == null) {
return false;
}
final int contentLength;
try {
contentLength = Integer.parseInt(hdr.getValue());
} catch (final NumberFormatException nfe) {
return false;
}
return (resource.length() < contentLength);
}
示例9: expiresHeaderLessOrEqualToDateHeaderAndNoCacheControl
import ch.boye.httpclientandroidlib.protocol.HTTP; //导入依赖的package包/类
private boolean expiresHeaderLessOrEqualToDateHeaderAndNoCacheControl(
final HttpResponse response) {
if (response.getFirstHeader(HeaderConstants.CACHE_CONTROL) != null) {
return false;
}
final Header expiresHdr = response.getFirstHeader(HeaderConstants.EXPIRES);
final Header dateHdr = response.getFirstHeader(HTTP.DATE_HEADER);
if (expiresHdr == null || dateHdr == null) {
return false;
}
final Date expires = DateUtils.parseDate(expiresHdr.getValue());
final Date date = DateUtils.parseDate(dateHdr.getValue());
if (expires == null || date == null) {
return false;
}
return expires.equals(date) || expires.before(date);
}
示例10: lineFromLineBuffer
import ch.boye.httpclientandroidlib.protocol.HTTP; //导入依赖的package包/类
/**
* Reads a complete line of characters up to a line delimiter from this
* session buffer. The line delimiter itself is discarded. If no char is
* available because the end of the stream has been reached,
* <code>null</code> is returned. This method blocks until input data is
* available, end of file is detected, or an exception is thrown.
* <p>
* This method treats a lone LF as a valid line delimiters in addition
* to CR-LF required by the HTTP specification.
*
* @return HTTP line as a string
* @exception IOException if an I/O error occurs.
*/
private int lineFromLineBuffer(final CharArrayBuffer charbuffer)
throws IOException {
// discard LF if found
int len = this.linebuffer.length();
if (len > 0) {
if (this.linebuffer.byteAt(len - 1) == HTTP.LF) {
len--;
}
// discard CR if found
if (len > 0) {
if (this.linebuffer.byteAt(len - 1) == HTTP.CR) {
len--;
}
}
}
if (this.ascii) {
charbuffer.append(this.linebuffer, 0, len);
} else {
final ByteBuffer bbuf = ByteBuffer.wrap(this.linebuffer.buffer(), 0, len);
len = appendDecoded(charbuffer, bbuf);
}
this.linebuffer.clear();
return len;
}
示例11: lineFromReadBuffer
import ch.boye.httpclientandroidlib.protocol.HTTP; //导入依赖的package包/类
private int lineFromReadBuffer(final CharArrayBuffer charbuffer, final int position)
throws IOException {
final int off = this.bufferpos;
int i = position;
this.bufferpos = i + 1;
if (i > off && this.buffer[i - 1] == HTTP.CR) {
// skip CR if found
i--;
}
int len = i - off;
if (this.ascii) {
charbuffer.append(this.buffer, off, len);
} else {
final ByteBuffer bbuf = ByteBuffer.wrap(this.buffer, off, len);
len = appendDecoded(charbuffer, bbuf);
}
return len;
}
示例12: lineFromLineBuffer
import ch.boye.httpclientandroidlib.protocol.HTTP; //导入依赖的package包/类
/**
* Reads a complete line of characters up to a line delimiter from this
* session buffer. The line delimiter itself is discarded. If no char is
* available because the end of the stream has been reached,
* <code>null</code> is returned. This method blocks until input data is
* available, end of file is detected, or an exception is thrown.
* <p>
* This method treats a lone LF as a valid line delimiters in addition
* to CR-LF required by the HTTP specification.
*
* @return HTTP line as a string
* @exception IOException if an I/O error occurs.
*/
private int lineFromLineBuffer(final CharArrayBuffer charbuffer)
throws IOException {
// discard LF if found
int len = this.linebuffer.length();
if (len > 0) {
if (this.linebuffer.byteAt(len - 1) == HTTP.LF) {
len--;
}
// discard CR if found
if (len > 0) {
if (this.linebuffer.byteAt(len - 1) == HTTP.CR) {
len--;
}
}
}
if (this.decoder == null) {
charbuffer.append(this.linebuffer, 0, len);
} else {
final ByteBuffer bbuf = ByteBuffer.wrap(this.linebuffer.buffer(), 0, len);
len = appendDecoded(charbuffer, bbuf);
}
this.linebuffer.clear();
return len;
}
示例13: lineFromReadBuffer
import ch.boye.httpclientandroidlib.protocol.HTTP; //导入依赖的package包/类
private int lineFromReadBuffer(final CharArrayBuffer charbuffer, final int position)
throws IOException {
int pos = position;
final int off = this.bufferpos;
int len;
this.bufferpos = pos + 1;
if (pos > off && this.buffer[pos - 1] == HTTP.CR) {
// skip CR if found
pos--;
}
len = pos - off;
if (this.decoder == null) {
charbuffer.append(this.buffer, off, len);
} else {
final ByteBuffer bbuf = ByteBuffer.wrap(this.buffer, off, len);
len = appendDecoded(charbuffer, bbuf);
}
return len;
}
示例14: createDefaultHttpParams
import ch.boye.httpclientandroidlib.protocol.HTTP; //导入依赖的package包/类
/**
* Creates default params setting the user agent.
*
* @return Basic HTTP parameters with a custom user agent
*/
protected HttpParams createDefaultHttpParams() {
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
String version = Version.getSpecification();
if (version == null) {
version = VersionInfo.UNAVAILABLE;
}
HttpProtocolParams.setUserAgent(params, "Sardine/" + version);
// Only selectively enable this for PUT but not all entity enclosing
// methods
HttpProtocolParams.setUseExpectContinue(params, false);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params,
HTTP.DEFAULT_CONTENT_CHARSET);
HttpConnectionParams.setTcpNoDelay(params, true);
HttpConnectionParams.setSocketBufferSize(params, 8192);
return params;
}
示例15: MultipartFormEntity
import ch.boye.httpclientandroidlib.protocol.HTTP; //导入依赖的package包/类
MultipartFormEntity(
final AbstractMultipartForm multipart,
final String contentType,
final long contentLength) {
super();
this.multipart = multipart;
this.contentType = new BasicHeader(HTTP.CONTENT_TYPE, contentType);
this.contentLength = contentLength;
}