本文整理汇总了Java中org.apache.http.client.methods.HttpEntityEnclosingRequestBase.getEntity方法的典型用法代码示例。如果您正苦于以下问题:Java HttpEntityEnclosingRequestBase.getEntity方法的具体用法?Java HttpEntityEnclosingRequestBase.getEntity怎么用?Java HttpEntityEnclosingRequestBase.getEntity使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.client.methods.HttpEntityEnclosingRequestBase
的用法示例。
在下文中一共展示了HttpEntityEnclosingRequestBase.getEntity方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doHandle
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; //导入方法依赖的package包/类
protected void doHandle(String uriId,HttpUriRequest request,Object result){
if(this.isError(result)){
String content = null;
if(request instanceof HttpEntityEnclosingRequestBase){
HttpEntityEnclosingRequestBase request_base = (HttpEntityEnclosingRequestBase)request;
HttpEntity entity = request_base.getEntity();
//MULTIPART_FORM_DATA 请求类型判断
if(entity.getContentType().toString().indexOf(ContentType.MULTIPART_FORM_DATA.getMimeType()) == -1){
try {
content = EntityUtils.toString(entity);
} catch (Exception e) {
e.printStackTrace();
}
}
if(logger.isErrorEnabled()){
logger.error("URI[{}] {} Content:{} Result:{}",
uriId,
request.getURI(),
content == null ? "multipart_form_data" : content,
result == null? null : JsonUtil.toJSONString(result));
}
}
this.handle(uriId,request.getURI().toString(),content,result);
}
}
示例2: testWithBody
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; //导入方法依赖的package包/类
@Test
public void testWithBody() throws Exception {
final InputStream bodyStream = mock(InputStream.class);
testBuilder.body(bodyStream, "plain/text")
.digestSha1("checksum")
.filename("file.txt")
.slug("slug_value")
.perform();
final ArgumentCaptor<HttpRequestBase> requestCaptor = ArgumentCaptor.forClass(HttpRequestBase.class);
verify(client).executeRequest(eq(uri), requestCaptor.capture());
final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue();
final HttpEntity bodyEntity = request.getEntity();
assertEquals(bodyStream, bodyEntity.getContent());
assertEquals("plain/text", request.getFirstHeader(CONTENT_TYPE).getValue());
assertEquals("sha1=checksum", request.getFirstHeader(DIGEST).getValue());
assertEquals("slug_value", request.getFirstHeader(SLUG).getValue());
assertEquals("attachment; filename=\"file.txt\"", request.getFirstHeader(CONTENT_DISPOSITION).getValue());
}
示例3: testWithBodyMultipleChecksums
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; //导入方法依赖的package包/类
@Test
public void testWithBodyMultipleChecksums() throws Exception {
final InputStream bodyStream = mock(InputStream.class);
testBuilder.body(bodyStream, "plain/text")
.digestSha1("checksum")
.digestSha256("checksum256")
.perform();
final ArgumentCaptor<HttpRequestBase> requestCaptor = ArgumentCaptor.forClass(HttpRequestBase.class);
verify(client).executeRequest(eq(uri), requestCaptor.capture());
final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue();
final HttpEntity bodyEntity = request.getEntity();
assertEquals(bodyStream, bodyEntity.getContent());
assertEquals("plain/text", request.getFirstHeader(CONTENT_TYPE).getValue());
assertEquals("sha1=checksum, sha256=checksum256", request.getFirstHeader(DIGEST).getValue());
}
示例4: testWithBody
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; //导入方法依赖的package包/类
@Test
public void testWithBody() throws Exception {
final InputStream bodyStream = mock(InputStream.class);
testBuilder.body(bodyStream, "plain/text")
.digestSha1("checksum")
.filename("file.txt")
.perform();
final ArgumentCaptor<HttpRequestBase> requestCaptor = ArgumentCaptor.forClass(HttpRequestBase.class);
verify(client).executeRequest(eq(uri), requestCaptor.capture());
final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue();
final HttpEntity bodyEntity = request.getEntity();
assertEquals(bodyStream, bodyEntity.getContent());
assertEquals("plain/text", request.getFirstHeader(CONTENT_TYPE).getValue());
assertEquals("sha1=checksum", request.getFirstHeader(DIGEST).getValue());
assertEquals("attachment; filename=\"file.txt\"", request.getFirstHeader(CONTENT_DISPOSITION).getValue());
}
示例5: testWithModificationHeaders
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; //导入方法依赖的package包/类
@Test
public void testWithModificationHeaders() throws Exception {
final InputStream bodyStream = mock(InputStream.class);
final String etag = "123456";
final String lastModified = "Mon, 19 May 2014 19:44:59 GMT";
testBuilder.body(bodyStream, "plain/text")
.ifMatch(etag)
.ifUnmodifiedSince(lastModified)
.perform();
final ArgumentCaptor<HttpRequestBase> requestCaptor = ArgumentCaptor.forClass(HttpRequestBase.class);
verify(client).executeRequest(eq(uri), requestCaptor.capture());
final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue();
final HttpEntity bodyEntity = request.getEntity();
assertEquals(bodyStream, bodyEntity.getContent());
assertEquals("plain/text", request.getFirstHeader(CONTENT_TYPE).getValue());
assertEquals(etag, request.getFirstHeader(IF_MATCH).getValue());
assertEquals(lastModified, request.getFirstHeader(IF_UNMODIFIED_SINCE).getValue());
}
示例6: loggerRequest
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; //导入方法依赖的package包/类
/**
* 日志记录
* @param request request
* @return log request id
*/
private static String loggerRequest(HttpUriRequest request){
String id = UUID.randomUUID().toString();
if(logger.isInfoEnabled()||logger.isDebugEnabled()){
if(request instanceof HttpEntityEnclosingRequestBase){
HttpEntityEnclosingRequestBase request_base = (HttpEntityEnclosingRequestBase)request;
HttpEntity entity = request_base.getEntity();
String content = null;
//MULTIPART_FORM_DATA 请求类型判断
if(entity.getContentType().toString().indexOf(ContentType.MULTIPART_FORM_DATA.getMimeType()) == -1){
try {
content = EntityUtils.toString(entity);
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage());
}
}
logger.info("URI[{}] {} {} ContentLength:{} Content:{}",
id,
request.getURI().toString(),
entity.getContentType(),
entity.getContentLength(),
content == null?"multipart_form_data":content);
}else{
logger.info("URI[{}] {}",id,request.getURI().toString());
}
}
return id;
}
示例7: testAttachment
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; //导入方法依赖的package包/类
@Test
public void testAttachment() throws Exception {
final InputStream bodyStream = mock(InputStream.class);
testBuilder.body(bodyStream, "plain/text").filename(null).perform();
final ArgumentCaptor<HttpRequestBase> requestCaptor = ArgumentCaptor.forClass(HttpRequestBase.class);
verify(client).executeRequest(eq(uri), requestCaptor.capture());
final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue();
final HttpEntity bodyEntity = request.getEntity();
assertEquals(bodyStream, bodyEntity.getContent());
assertEquals("attachment", request.getFirstHeader(CONTENT_DISPOSITION).getValue());
}
示例8: testBodyNoType
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; //导入方法依赖的package包/类
@Test
public void testBodyNoType() throws Exception {
final InputStream bodyStream = mock(InputStream.class);
testBuilder.body(bodyStream).perform();
final ArgumentCaptor<HttpRequestBase> requestCaptor = ArgumentCaptor.forClass(HttpRequestBase.class);
verify(client).executeRequest(eq(uri), requestCaptor.capture());
final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue();
final HttpEntity bodyEntity = request.getEntity();
assertEquals(bodyStream, bodyEntity.getContent());
assertEquals("application/octet-stream", request.getFirstHeader(CONTENT_TYPE).getValue());
}
示例9: testDisposition
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; //导入方法依赖的package包/类
@Test
public void testDisposition() throws Exception {
final InputStream bodyStream = mock(InputStream.class);
testBuilder.body(bodyStream, "plain/text").filename(null).perform();
final ArgumentCaptor<HttpRequestBase> requestCaptor = ArgumentCaptor.forClass(HttpRequestBase.class);
verify(client).executeRequest(eq(uri), requestCaptor.capture());
final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue();
final HttpEntity bodyEntity = request.getEntity();
assertEquals(bodyStream, bodyEntity.getContent());
assertEquals("attachment", request.getFirstHeader(CONTENT_DISPOSITION).getValue());
}
示例10: testSearchAutomaticallyUsesPost
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; //导入方法依赖的package包/类
@SuppressWarnings("unused")
@Test
public void testSearchAutomaticallyUsesPost() throws Exception {
String msg = getPatientFeedWithOneResult();
ArgumentCaptor<HttpUriRequest> capt = ArgumentCaptor.forClass(HttpUriRequest.class);
when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
when(myHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK"));
when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_FHIR_XML + "; charset=UTF-8"));
when(myHttpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(msg), Charset.forName("UTF-8")));
IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir");
String longValue = StringUtils.leftPad("", 20000, 'B');
//@formatter:off
Bundle response = client.search()
.forResource("Patient")
.where(Patient.NAME.matches().value(longValue))
.execute();
//@formatter:on
assertEquals("http://example.com/fhir/Patient/_search", capt.getValue().getURI().toString());
HttpEntityEnclosingRequestBase enc = (HttpEntityEnclosingRequestBase) capt.getValue();
UrlEncodedFormEntity ent = (UrlEncodedFormEntity) enc.getEntity();
String string = IOUtils.toString(ent.getContent());
ourLog.info(string);
assertEquals("name=" + longValue, string);
}
示例11: testSearchUsingPost
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; //导入方法依赖的package包/类
@SuppressWarnings("unused")
@Test
public void testSearchUsingPost() throws Exception {
String msg = getPatientFeedWithOneResult();
ArgumentCaptor<HttpUriRequest> capt = ArgumentCaptor.forClass(HttpUriRequest.class);
when(myHttpClient.execute(capt.capture())).thenReturn(myHttpResponse);
when(myHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK"));
when(myHttpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_FHIR_XML + "; charset=UTF-8"));
when(myHttpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(msg), Charset.forName("UTF-8")));
IGenericClient client = ourCtx.newRestfulGenericClient("http://example.com/fhir");
//@formatter:off
Bundle response = client.search()
.forResource("Patient")
.where(Patient.NAME.matches().value("james"))
.usingStyle(SearchStyleEnum.POST)
.execute();
//@formatter:on
assertEquals("http://example.com/fhir/Patient/_search", capt.getValue().getURI().toString());
HttpEntityEnclosingRequestBase enc = (HttpEntityEnclosingRequestBase) capt.getValue();
UrlEncodedFormEntity ent = (UrlEncodedFormEntity) enc.getEntity();
String string = IOUtils.toString(ent.getContent());
ourLog.info(string);
assertEquals("name=james", string);
}
示例12: sendEntityData
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; //导入方法依赖的package包/类
/**
* Creates the entity data to be sent.
* <p>
* If there is a file entry with a non-empty MIME type we use that to
* set the request Content-Type header, otherwise we default to whatever
* header is present from a Header Manager.
* <p>
* If the content charset {@link #getContentEncoding()} is null or empty
* we use the HC4 default provided by {@link HTTP#DEF_CONTENT_CHARSET} which is
* ISO-8859-1.
*
* @param entity to be processed, e.g. PUT or PATCH
* @return the entity content, may be empty
* @throws UnsupportedEncodingException for invalid charset name
* @throws IOException cannot really occur for ByteArrayOutputStream methods
*/
protected String sendEntityData( HttpEntityEnclosingRequestBase entity) throws IOException {
// Buffer to hold the entity body
StringBuilder entityBody = new StringBuilder(1000);
boolean hasEntityBody = false;
final HTTPFileArg[] files = getHTTPFiles();
// Allow the mimetype of the file to control the content type
// This is not obvious in GUI if you are not uploading any files,
// but just sending the content of nameless parameters
final HTTPFileArg file = files.length > 0? files[0] : null;
String contentTypeValue = null;
if(file != null && file.getMimeType() != null && file.getMimeType().length() > 0) {
contentTypeValue = file.getMimeType();
entity.setHeader(HEADER_CONTENT_TYPE, contentTypeValue); // we provide the MIME type here
}
// Check for local contentEncoding (charset) override; fall back to default for content body
// we do this here rather so we can use the same charset to retrieve the data
final String charset = getContentEncoding(HTTP.DEF_CONTENT_CHARSET.name());
// Only create this if we are overriding whatever default there may be
// If there are no arguments, we can send a file as the body of the request
if(!hasArguments() && getSendFileAsPostBody()) {
hasEntityBody = true;
// If getSendFileAsPostBody returned true, it's sure that file is not null
File reservedFile = FileServer.getFileServer().getResolvedFile(files[0].getPath());
FileEntity fileRequestEntity = new FileEntity(reservedFile); // no need for content-type here
entity.setEntity(fileRequestEntity);
}
// If none of the arguments have a name specified, we
// just send all the values as the entity body
else if(getSendParameterValuesAsPostBody()) {
hasEntityBody = true;
// Just append all the parameter values, and use that as the entity body
StringBuilder entityBodyContent = new StringBuilder();
for (JMeterProperty jMeterProperty : getArguments()) {
HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
// Note: if "Encoded?" is not selected, arg.getEncodedValue is equivalent to arg.getValue
if (charset != null) {
entityBodyContent.append(arg.getEncodedValue(charset));
} else {
entityBodyContent.append(arg.getEncodedValue());
}
}
StringEntity requestEntity = new StringEntity(entityBodyContent.toString(), charset);
entity.setEntity(requestEntity);
}
// Check if we have any content to send for body
if(hasEntityBody) {
// If the request entity is repeatable, we can send it first to
// our own stream, so we can return it
final HttpEntity entityEntry = entity.getEntity();
if(entityEntry.isRepeatable()) {
entityBody.append("<actual file content, not shown here>");
}
else { // this probably cannot happen
entityBody.append("<RequestEntity was not repeatable, cannot view what was sent>");
}
}
return entityBody.toString(); // may be the empty string
}