本文整理匯總了Java中org.apache.http.entity.BasicHttpEntity.setContent方法的典型用法代碼示例。如果您正苦於以下問題:Java BasicHttpEntity.setContent方法的具體用法?Java BasicHttpEntity.setContent怎麽用?Java BasicHttpEntity.setContent使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.http.entity.BasicHttpEntity
的用法示例。
在下文中一共展示了BasicHttpEntity.setContent方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testExecute_non2xx_exception
import org.apache.http.entity.BasicHttpEntity; //導入方法依賴的package包/類
@Test
public void testExecute_non2xx_exception() throws IOException {
HttpResponse resp = new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, 404, "Not found"));
BasicHttpEntity entity = new BasicHttpEntity();
entity.setContent(new ByteArrayInputStream("{\"message\" : \"error payload\"}".getBytes()));
resp.setEntity(entity);
Mockito.doReturn(resp).when(mockClient).execute(any(HttpUriRequest.class), any(HttpContext.class));
Map<String, String> headers = new HashMap<>();
headers.put("Account-Id", "fubar");
headers.put("Content-Type", "application/json");
try {
client.execute(
new GenericApiGatewayRequestBuilder()
.withBody(new ByteArrayInputStream("test request".getBytes()))
.withHttpMethod(HttpMethodName.POST)
.withHeaders(headers)
.withResourcePath("/test/orders").build());
Assert.fail("Expected exception");
} catch (GenericApiGatewayException e) {
assertEquals("Wrong status code", 404, e.getStatusCode());
assertEquals("Wrong exception message", "{\"message\":\"error payload\"}", e.getErrorMessage());
}
}
示例2: entityFromConnection
import org.apache.http.entity.BasicHttpEntity; //導入方法依賴的package包/類
/**
* Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
* @param connection
* @return an HttpEntity populated with data from <code>connection</code>.
*/
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
BasicHttpEntity entity = new BasicHttpEntity();
InputStream inputStream;
try {
inputStream = connection.getInputStream();
} catch (IOException ioe) {
inputStream = connection.getErrorStream();
}
entity.setContent(inputStream);
entity.setContentLength(connection.getContentLength());
entity.setContentEncoding(connection.getContentEncoding());
entity.setContentType(connection.getContentType());
return entity;
}
示例3: pushContent
import org.apache.http.entity.BasicHttpEntity; //導入方法依賴的package包/類
private void pushContent(HttpUriRequest request, String contentType, String contentEncoding, byte[] content) {
// TODO: check other preconditions?
if (contentType != null && content != null && request instanceof HttpEntityEnclosingRequest) {
BasicHttpEntity entity = new BasicHttpEntity();
entity.setContent(new ByteArrayInputStream(content));
entity.setContentLength(content.length);
entity.setChunked(false);
if (contentEncoding != null)
entity.setContentEncoding(contentEncoding);
entity.setContentType(contentType);
HttpEntityEnclosingRequest rr = (HttpEntityEnclosingRequest) request;
rr.setEntity(entity);
}
}
示例4: entityFromConnection
import org.apache.http.entity.BasicHttpEntity; //導入方法依賴的package包/類
/**
* Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
*
* @return an HttpEntity populated with data from <code>connection</code>.
*/
/*
* 通過一個 HttpURLConnection 獲取其對應的 HttpEntity ( 這裏就 HttpEntity 而言,耦合了 Apache )
*/
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
BasicHttpEntity entity = new BasicHttpEntity();
InputStream inputStream;
try {
inputStream = connection.getInputStream();
} catch (IOException ioe) {
inputStream = connection.getErrorStream();
}
// 設置 HttpEntity 的內容
entity.setContent(inputStream);
// 設置 HttpEntity 的長度
entity.setContentLength(connection.getContentLength());
// 設置 HttpEntity 的編碼
entity.setContentEncoding(connection.getContentEncoding());
// 設置 HttpEntity Content-Type
entity.setContentType(connection.getContentType());
return entity;
}
示例5: entityFromConnection
import org.apache.http.entity.BasicHttpEntity; //導入方法依賴的package包/類
/**
* 獲取響應報文實體
*
* @return
* @throws IOException
*/
private HttpEntity entityFromConnection() throws IOException {
BasicHttpEntity entity = new BasicHttpEntity();
InputStream inputStream;
try {
inputStream = connection.getInputStream();
} catch (IOException ioe) {
inputStream = connection.getErrorStream();
}
if (GZIP.equals(getResponseheader(ResponseHeader.HEADER_CONTENT_ENCODING))) {
entity.setContent(new GZIPInputStream(inputStream));
} else {
entity.setContent(inputStream);
}
entity.setContentLength(connection.getContentLength());
entity.setContentEncoding(connection.getContentEncoding());
entity.setContentType(connection.getContentType());
return entity;
}
示例6: getEntityFromConnection
import org.apache.http.entity.BasicHttpEntity; //導入方法依賴的package包/類
private HttpEntity getEntityFromConnection(HttpURLConnection connection) {
BasicHttpEntity entity = new BasicHttpEntity();
InputStream inputStream;
try {
inputStream = connection.getInputStream();
} catch (IOException e) {
inputStream = connection.getErrorStream();
}
entity.setContent(inputStream);
entity.setContentLength(connection.getContentLength());
entity.setContentEncoding(connection.getContentEncoding());
entity.setContentType(connection.getContentType());
return entity;
}
示例7: handleResponseTest
import org.apache.http.entity.BasicHttpEntity; //導入方法依賴的package包/類
/**
*
*/
@Test
public void handleResponseTest() throws Exception {
ResponseHandler handler = new ResponseHandler();
HttpResponseFactory factory = new DefaultHttpResponseFactory();
HttpResponse responseSent = factory.newHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK,
"reason"), null);
HttpResponse response = handler.handleResponse(responseSent);
Assert.assertEquals(handler.getStatus(), HttpStatus.SC_OK);
Assert.assertFalse(handler.hasContent());
// response with content.
BasicHttpEntity entity = new BasicHttpEntity();
InputStream inputStream = new ByteArrayInputStream("new content".getBytes());
entity.setContent(inputStream);
entity.setContentLength("new content".length()); // sets the length
response.setEntity(entity);
response = handler.handleResponse(responseSent);
Assert.assertEquals("new content", handler.getResponseContent());
}
示例8: shouldSuccessfullyProcessJson
import org.apache.http.entity.BasicHttpEntity; //導入方法依賴的package包/類
@Test
public void shouldSuccessfullyProcessJson() throws ExecutionException, InterruptedException, IOException {
JsonObject value = new JsonObject("unhappy", "allen");
String serialized = mapper.writeValueAsString(value);
ByteArrayInputStream contentInputStream = new ByteArrayInputStream(serialized.getBytes(Charsets.US_ASCII));
BasicHttpEntity entity = new BasicHttpEntity();
entity.setContent(contentInputStream);
HttpPost post = new HttpPost(ServiceConfiguration.SERVICE_URL + "/consumer");
post.setHeader(new BasicHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON));
post.setEntity(entity);
Future<HttpResponse> future = client.getClient().execute(post, null);
HttpResponse response = future.get();
assertThat(response.getStatusLine().getStatusCode(), equalTo(HttpStatus.SC_OK));
assertThat(HttpUtils.readStreamToString(response.getEntity().getContent()), equalTo(getResponseValue(value)));
}
示例9: entityFromConnection
import org.apache.http.entity.BasicHttpEntity; //導入方法依賴的package包/類
/**
* Initializes an {@link HttpEntity} from the given
* {@link HttpURLConnection}.
*
* @param connection
* @return an HttpEntity populated with data from <code>connection</code>.
*/
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
BasicHttpEntity entity = new BasicHttpEntity();
InputStream inputStream;
try {
inputStream = connection.getInputStream();
} catch (IOException ioe) {
inputStream = connection.getErrorStream();
}
entity.setContent(inputStream);
entity.setContentLength(connection.getContentLength());
entity.setContentEncoding(connection.getContentEncoding());
entity.setContentType(connection.getContentType());
return entity;
}
示例10: runLDPIndirectContainerTestSuite
import org.apache.http.entity.BasicHttpEntity; //導入方法依賴的package包/類
@Test
public void runLDPIndirectContainerTestSuite() throws IOException {
final String pid = "ldp-test-indirect-" + UUID.randomUUID().toString();
final HttpPut request = new HttpPut(serverAddress + pid);
final BasicHttpEntity entity = new BasicHttpEntity();
entity.setContent(IOUtils.toInputStream("<> a <" + INDIRECT_CONTAINER + ">;" +
" <" + LDP_NAMESPACE + "membershipResource> <> ;" +
" <" + LDP_NAMESPACE + "insertedContentRelation> <" + LDP_NAMESPACE + "MemberSubject> ;" +
" <" + LDP_NAMESPACE + "hasMemberRelation> <" + LDP_NAMESPACE + "member> ."));
request.setEntity(entity);
request.setHeader(CONTENT_TYPE, "text/turtle");
try (final CloseableHttpResponse response = executeWithBasicAuth(request)) {
assertEquals(CREATED.getStatusCode(), response.getStatusLine().getStatusCode());
final HashMap<String, String> options = new HashMap<>();
options.put("server", serverAddress + pid);
options.put("output", "report-indirect");
options.put("indirect", "true");
options.put("non-rdf", "true");
options.put("read-only-prop", "http://fedora.info/definitions/v4/repository#uuid");
final LdpTestSuite testSuite = new LdpTestSuite(options);
testSuite.run();
assertTrue("The LDP test suite is only informational", true);
}
}
示例11: runLDPBasicContainerTestSuite
import org.apache.http.entity.BasicHttpEntity; //導入方法依賴的package包/類
@Test
public void runLDPBasicContainerTestSuite() throws IOException {
final String pid = "ldp-test-basic-" + UUID.randomUUID().toString();
final HttpPut request = new HttpPut(serverAddress + pid);
final BasicHttpEntity entity = new BasicHttpEntity();
entity.setContent(IOUtils.toInputStream("<> a <" + BASIC_CONTAINER + "> ."));
request.setEntity(entity);
request.setHeader(CONTENT_TYPE, "text/turtle");
try (final CloseableHttpResponse response = executeWithBasicAuth(request)) {
assertEquals(CREATED.getStatusCode(), response.getStatusLine().getStatusCode());
final HashMap<String, String> options = new HashMap<>();
options.put("server", serverAddress + pid);
options.put("output", "report-basic");
options.put("basic", "true");
options.put("non-rdf", "true");
options.put("read-only-prop", "http://fedora.info/definitions/v4/repository#uuid");
final LdpTestSuite testSuite = new LdpTestSuite(options);
testSuite.run();
assertTrue("The LDP test suite is only informational", true);
}
}
示例12: createOkResponseWithForm
import org.apache.http.entity.BasicHttpEntity; //導入方法依賴的package包/類
private BasicHttpResponse createOkResponseWithForm() {
BasicHttpResponse okayResponseWithForm = createOkResponse();
BasicHttpEntity okayResponseWithFormEntity = new BasicHttpEntity();
String form = "<form action=\"owaauth.dll\"></form>";
okayResponseWithFormEntity.setContent(new ByteArrayInputStream(form.getBytes()));
okayResponseWithForm.setEntity(okayResponseWithFormEntity);
return okayResponseWithForm;
}
示例13: entityFromConnection
import org.apache.http.entity.BasicHttpEntity; //導入方法依賴的package包/類
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
InputStream inputStream;
BasicHttpEntity entity = new BasicHttpEntity();
try {
inputStream = connection.getInputStream();
} catch (IOException e) {
inputStream = connection.getErrorStream();
}
entity.setContent(inputStream);
entity.setContentLength((long) connection.getContentLength());
entity.setContentEncoding(connection.getContentEncoding());
entity.setContentType(connection.getContentType());
return entity;
}
示例14: entityFromOkHttpResponse
import org.apache.http.entity.BasicHttpEntity; //導入方法依賴的package包/類
private static HttpEntity entityFromOkHttpResponse(Response r) throws IOException {
BasicHttpEntity entity = new BasicHttpEntity();
ResponseBody body = r.body();
entity.setContent(body.byteStream());
entity.setContentLength(body.contentLength());
entity.setContentEncoding(r.header("Content-Encoding"));
if (body.contentType() != null) {
entity.setContentType(body.contentType().type());
}
return entity;
}
示例15: buildLogHttpRequest
import org.apache.http.entity.BasicHttpEntity; //導入方法依賴的package包/類
private HttpRequest buildLogHttpRequest(String content) throws UnsupportedEncodingException {
BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/log");
BasicHttpEntity entity = new BasicHttpEntity();
entity.setContent(new ByteArrayInputStream(content.getBytes("UTF-8")));
request.setEntity(entity);
return request;
}