本文整理汇总了Java中ch.boye.httpclientandroidlib.HttpResponse.setEntity方法的典型用法代码示例。如果您正苦于以下问题:Java HttpResponse.setEntity方法的具体用法?Java HttpResponse.setEntity怎么用?Java HttpResponse.setEntity使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ch.boye.httpclientandroidlib.HttpResponse
的用法示例。
在下文中一共展示了HttpResponse.setEntity方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: handleException
import ch.boye.httpclientandroidlib.HttpResponse; //导入方法依赖的package包/类
/**
* Handles the given exception and generates an HTTP response to be sent
* back to the client to inform about the exceptional condition encountered
* in the course of the request processing.
*
* @param ex the exception.
* @param response the HTTP response.
*/
protected void handleException(final HttpException ex, final HttpResponse response) {
if (ex instanceof MethodNotSupportedException) {
response.setStatusCode(HttpStatus.SC_NOT_IMPLEMENTED);
} else if (ex instanceof UnsupportedHttpVersionException) {
response.setStatusCode(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED);
} else if (ex instanceof ProtocolException) {
response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
} else {
response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
}
String message = ex.getMessage();
if (message == null) {
message = ex.toString();
}
final byte[] msg = EncodingUtils.getAsciiBytes(message);
final ByteArrayEntity entity = new ByteArrayEntity(msg);
entity.setContentType("text/plain; charset=US-ASCII");
response.setEntity(entity);
}
示例2: generateResponse
import ch.boye.httpclientandroidlib.HttpResponse; //导入方法依赖的package包/类
/**
* If I was able to use a {@link CacheEntity} to response to the {@link ch.boye.httpclientandroidlib.HttpRequest} then
* generate an {@link HttpResponse} based on the cache entry.
* @param entry
* {@link CacheEntity} to transform into an {@link HttpResponse}
* @return {@link HttpResponse} that was constructed
*/
CloseableHttpResponse generateResponse(final HttpCacheEntry entry) {
final Date now = new Date();
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, entry
.getStatusCode(), entry.getReasonPhrase());
response.setHeaders(entry.getAllHeaders());
if (entry.getResource() != null) {
final HttpEntity entity = new CacheEntity(entry);
addMissingContentLengthHeader(response, entity);
response.setEntity(entity);
}
final long age = this.validityStrategy.getCurrentAgeSecs(entry, now);
if (age > 0) {
if (age >= Integer.MAX_VALUE) {
response.setHeader(HeaderConstants.AGE, "2147483648");
} else {
response.setHeader(HeaderConstants.AGE, "" + ((int) age));
}
}
return Proxies.enhanceResponse(response);
}
示例3: getReconstructedResponse
import ch.boye.httpclientandroidlib.HttpResponse; //导入方法依赖的package包/类
CloseableHttpResponse getReconstructedResponse() throws IOException {
ensureConsumed();
final HttpResponse reconstructed = new BasicHttpResponse(response.getStatusLine());
reconstructed.setHeaders(response.getAllHeaders());
final CombinedEntity combinedEntity = new CombinedEntity(resource, instream);
final HttpEntity entity = response.getEntity();
if (entity != null) {
combinedEntity.setContentType(entity.getContentType());
combinedEntity.setContentEncoding(entity.getContentEncoding());
combinedEntity.setChunked(entity.isChunked());
}
reconstructed.setEntity(combinedEntity);
return (CloseableHttpResponse) Proxy.newProxyInstance(
ResponseProxyHandler.class.getClassLoader(),
new Class<?>[] { CloseableHttpResponse.class },
new ResponseProxyHandler(reconstructed) {
@Override
public void close() throws IOException {
response.close();
}
});
}
示例4: process
import ch.boye.httpclientandroidlib.HttpResponse; //导入方法依赖的package包/类
/**
* Handles the following {@code Content-Encoding}s by
* using the appropriate decompressor to wrap the response Entity:
* <ul>
* <li>gzip - see {@link GzipDecompressingEntity}</li>
* <li>deflate - see {@link DeflateDecompressingEntity}</li>
* <li>identity - no action needed</li>
* </ul>
*
* @param response the response which contains the entity
* @param context not currently used
*
* @throws HttpException if the {@code Content-Encoding} is none of the above
*/
public void process(
final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
final HttpEntity entity = response.getEntity();
// entity can be null in case of 304 Not Modified, 204 No Content or similar
// check for zero length entity.
if (entity != null && entity.getContentLength() != 0) {
final Header ceheader = entity.getContentEncoding();
if (ceheader != null) {
final HeaderElement[] codecs = ceheader.getElements();
boolean uncompressed = false;
for (final HeaderElement codec : codecs) {
final String codecname = codec.getName().toLowerCase(Locale.ENGLISH);
if ("gzip".equals(codecname) || "x-gzip".equals(codecname)) {
response.setEntity(new GzipDecompressingEntity(response.getEntity()));
uncompressed = true;
break;
} else if ("deflate".equals(codecname)) {
response.setEntity(new DeflateDecompressingEntity(response.getEntity()));
uncompressed = true;
break;
} else if ("identity".equals(codecname)) {
/* Don't need to transform the content - no-op */
return;
} else {
throw new HttpException("Unsupported Content-Coding: " + codec.getName());
}
}
if (uncompressed) {
response.removeHeaders("Content-Length");
response.removeHeaders("Content-Encoding");
response.removeHeaders("Content-MD5");
}
}
}
}
示例5: receiveResponseEntity
import ch.boye.httpclientandroidlib.HttpResponse; //导入方法依赖的package包/类
public void receiveResponseEntity(final HttpResponse response)
throws HttpException, IOException {
Args.notNull(response, "HTTP response");
assertOpen();
final HttpEntity entity = this.entitydeserializer.deserialize(this.inbuffer, response);
response.setEntity(entity);
}
示例6: generateIncompleteResponseError
import ch.boye.httpclientandroidlib.HttpResponse; //导入方法依赖的package包/类
CloseableHttpResponse generateIncompleteResponseError(
final HttpResponse response, final Resource resource) {
final int contentLength = Integer.parseInt(response.getFirstHeader(HTTP.CONTENT_LEN).getValue());
final HttpResponse error =
new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_BAD_GATEWAY, "Bad Gateway");
error.setHeader("Content-Type","text/plain;charset=UTF-8");
final String msg = String.format("Received incomplete response " +
"with Content-Length %d but actual body length %d",
contentLength, resource.length());
final byte[] msgBytes = msg.getBytes();
error.setHeader("Content-Length", Integer.toString(msgBytes.length));
error.setEntity(new ByteArrayEntity(msgBytes));
return Proxies.enhanceResponse(error);
}
示例7: receiveResponseEntity
import ch.boye.httpclientandroidlib.HttpResponse; //导入方法依赖的package包/类
public void receiveResponseEntity(
final HttpResponse response) throws HttpException, IOException {
Args.notNull(response, "HTTP response");
ensureOpen();
final HttpEntity entity = prepareInput(response);
response.setEntity(entity);
}
示例8: enchance
import ch.boye.httpclientandroidlib.HttpResponse; //导入方法依赖的package包/类
public static void enchance(final HttpResponse response, final ConnectionHolder connHolder) {
final HttpEntity entity = response.getEntity();
if (entity != null && entity.isStreaming() && connHolder != null) {
response.setEntity(new ResponseEntityProxy(entity, connHolder));
}
}
示例9: ensureProtocolCompliance
import ch.boye.httpclientandroidlib.HttpResponse; //导入方法依赖的package包/类
/**
* When we get a response from a down stream server (Origin Server)
* we attempt to see if it is HTTP 1.1 Compliant and if not, attempt to
* make it so.
*
* @param request The {@link HttpRequest} that generated an origin hit and response
* @param response The {@link HttpResponse} from the origin server
* @throws IOException Bad things happened
*/
public void ensureProtocolCompliance(final HttpRequestWrapper request, final HttpResponse response)
throws IOException {
if (backendResponseMustNotHaveBody(request, response)) {
consumeBody(response);
response.setEntity(null);
}
requestDidNotExpect100ContinueButResponseIsOne(request, response);
transferEncodingIsNotReturnedTo1_0Client(request, response);
ensurePartialContentIsNotSentToAClientThatDidNotRequestIt(request, response);
ensure200ForOPTIONSRequestWithNoBodyHasContentLengthZero(request, response);
ensure206ContainsDateHeader(response);
ensure304DoesNotContainExtraEntityHeaders(response);
identityIsNotUsedInContentEncoding(response);
warningsWithNonMatchingWarnDatesAreRemoved(response);
}
示例10: updateEntity
import ch.boye.httpclientandroidlib.HttpResponse; //导入方法依赖的package包/类
/**
* Updates an entity in a response by first consuming an existing entity, then setting the new one.
*
* @param response the response with an entity to update; must not be null.
* @param entity the entity to set in the response.
* @throws IOException if an error occurs while reading the input stream on the existing
* entity.
* @throws IllegalArgumentException if response is null.
*
* @since 4.3
*/
public static void updateEntity(
final HttpResponse response, final HttpEntity entity) throws IOException {
Args.notNull(response, "Response");
consume(response.getEntity());
response.setEntity(entity);
}