本文整理汇总了Java中org.apache.http.HttpEntity.writeTo方法的典型用法代码示例。如果您正苦于以下问题:Java HttpEntity.writeTo方法的具体用法?Java HttpEntity.writeTo怎么用?Java HttpEntity.writeTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.HttpEntity
的用法示例。
在下文中一共展示了HttpEntity.writeTo方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: post
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
/**
* post 请求
*
* @param url
* @param xml
* @return
*/
private static String post(String url, String xml) {
try {
HttpEntity entity = Request.Post(url).bodyString(xml, ContentType.create("text/xml", Consts.UTF_8.name())).execute().returnResponse().getEntity();
if (entity != null) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
entity.writeTo(byteArrayOutputStream);
return byteArrayOutputStream.toString(Consts.UTF_8.name());
}
return null;
} catch (Exception e) {
logger.error("post请求异常," + e.getMessage() + "\npost url:" + url);
e.printStackTrace();
}
return null;
}
示例2: getResponseEntity
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
private String getResponseEntity(HttpResponse result) throws IOException {
HttpEntity entity = result.getEntity();
if (entity == null) {
log.debug("Null response entity");
return null;
}
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
entity.writeTo(bos);
byte[] bytes = bos.toByteArray();
if (bytes == null) {
bytes = "null".getBytes();
}
String response = new String(bytes);
log.debug("Response with code " + result + ": " + response);
return response;
} finally {
InputStream content = entity.getContent();
if (content != null) {
content.close();
}
}
}
示例3: process
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
@Override
public void process(final HttpRequest request,
final HttpContext context) throws HttpException, IOException {
final HttpRequestAttachment.Builder builder = create("Request", request.getRequestLine().getUri())
.withMethod(request.getRequestLine().getMethod());
Stream.of(request.getAllHeaders())
.forEach(header -> builder.withHeader(header.getName(), header.getValue()));
if (request instanceof HttpEntityEnclosingRequest) {
final HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
final ByteArrayOutputStream os = new ByteArrayOutputStream();
entity.writeTo(os);
final String body = new String(os.toByteArray(), StandardCharsets.UTF_8);
builder.withBody(body);
}
final HttpRequestAttachment requestAttachment = builder.build();
processor.addAttachment(requestAttachment, renderer);
}
示例4: MatrixHttpContentResult
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
public MatrixHttpContentResult(CloseableHttpResponse response) throws IOException {
HttpEntity entity = response.getEntity();
valid = entity != null && response.getStatusLine().getStatusCode() == 200;
if (entity != null) {
headers = Arrays.asList(response.getAllHeaders());
Header contentTypeHeader = entity.getContentType();
if (contentTypeHeader != null) {
contentType = Optional.of(contentTypeHeader.getValue());
} else {
contentType = Optional.empty();
}
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
entity.writeTo(outStream);
data = outStream.toByteArray();
} else {
headers = new ArrayList<>();
contentType = Optional.empty();
data = new byte[0];
}
}
示例5: serialize
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
/**
* Writes out the content of the given HTTP entity to the session output
* buffer based on properties of the given HTTP message.
*
* @param outbuffer the output session buffer.
* @param message the HTTP message.
* @param entity the HTTP entity to be written out.
* @throws HttpException in case of HTTP protocol violation.
* @throws IOException in case of an I/O error.
*/
public void serialize(
final SessionOutputBuffer outbuffer,
final HttpMessage message,
final HttpEntity entity) throws HttpException, IOException {
if (outbuffer == null) {
throw new IllegalArgumentException("Session output buffer may not be null");
}
if (message == null) {
throw new IllegalArgumentException("HTTP message may not be null");
}
if (entity == null) {
throw new IllegalArgumentException("HTTP entity may not be null");
}
OutputStream outstream = doSerialize(outbuffer, message);
entity.writeTo(outstream);
outstream.close();
}
示例6: toBody
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
public static HttpBody toBody(HttpEntity entity) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
entity.writeTo(baos);
return HttpBody.bytes(baos.toByteArray(), entity.getContentType().getValue());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例7: LoggingEntityWrapper
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
public LoggingEntityWrapper(HttpEntity wrappedEntity) {
super(wrappedEntity);
try {
ByteArrayOutputStream temp = new ByteArrayOutputStream();
wrappedEntity.writeTo(temp);
bytes = temp.toByteArray();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例8: getBytes
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
public static byte[] getBytes(String url) throws IOException {
HttpGet httpget = new HttpGet(url);
CloseableHttpClient httpClient = buildClient();
HttpResponse response = httpClient.execute(httpget);
HttpEntity entity = response.getEntity();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
entity.writeTo(baos);
return baos.toByteArray();
}
示例9: copyResponseEntity
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
/** Copy response body data (the entity) from the proxy to the servlet client. */
protected void copyResponseEntity(HttpResponse proxyResponse, HttpServletResponse servletResponse) throws IOException {
HttpEntity entity = proxyResponse.getEntity();
if (entity != null) {
OutputStream servletOutputStream = servletResponse.getOutputStream();
entity.writeTo(servletOutputStream);
}
}
示例10: toCurl
import org.apache.http.HttpEntity; //导入方法依赖的package包/类
/**
* Generates a cURL command equivalent to the given request.
*/
private static String toCurl(HttpUriRequest request, boolean logAuthToken) throws IOException {
StringBuilder builder = new StringBuilder();
builder.append("curl ");
for (Header header: request.getAllHeaders()) {
if (!logAuthToken
&& (header.getName().equals("Authorization") ||
header.getName().equals("Cookie"))) {
continue;
}
builder.append("--header \"");
builder.append(header.toString().trim());
builder.append("\" ");
}
URI uri = request.getURI();
// If this is a wrapped request, use the URI from the original
// request instead. getURI() on the wrapper seems to return a
// relative URI. We want an absolute URI.
if (request instanceof RequestWrapper) {
HttpRequest original = ((RequestWrapper) request).getOriginal();
if (original instanceof HttpUriRequest) {
uri = ((HttpUriRequest) original).getURI();
}
}
builder.append("\"");
builder.append(uri);
builder.append("\"");
if (request instanceof HttpEntityEnclosingRequest) {
HttpEntityEnclosingRequest entityRequest =
(HttpEntityEnclosingRequest) request;
HttpEntity entity = entityRequest.getEntity();
if (entity != null && entity.isRepeatable()) {
if (entity.getContentLength() < 1024) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
entity.writeTo(stream);
String entityString = stream.toString();
// TODO: Check the content type, too.
builder.append(" --data-ascii \"")
.append(entityString)
.append("\"");
} else {
builder.append(" [TOO MUCH DATA TO INCLUDE]");
}
}
}
return builder.toString();
}