當前位置: 首頁>>代碼示例>>Java>>正文


Java BasicHttpEntity.setContent方法代碼示例

本文整理匯總了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());
    }
}
 
開發者ID:rpgreen,項目名稱:apigateway-generic-java-sdk,代碼行數:27,代碼來源:GenericApiGatewayClientTest.java

示例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;
}
 
開發者ID:pooyafaroka,項目名稱:PlusGram,代碼行數:20,代碼來源:HurlStack.java

示例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);
		}

	}
 
開發者ID:RestComm,項目名稱:camelgateway,代碼行數:17,代碼來源:CamelGatewaySbb.java

示例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;
}
 
開發者ID:CaMnter,項目名稱:SaveVolley,代碼行數:27,代碼來源:HurlStack.java

示例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;
}
 
開發者ID:Leaking,項目名稱:WeGit,代碼行數:25,代碼來源:HttpKnife.java

示例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;
}
 
開發者ID:ppdai,項目名稱:BaijiClient4Android,代碼行數:17,代碼來源:HurlStack.java

示例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());
}
 
開發者ID:conwetlab,項目名稱:fiware-rss,代碼行數:22,代碼來源:ResponseHandlerTest.java

示例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)));
}
 
開發者ID:redbooth,項目名稱:baseline,代碼行數:19,代碼來源:TestJSONHandling.java

示例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;
}
 
開發者ID:barterli,項目名稱:barterli_android,代碼行數:23,代碼來源:HurlStack.java

示例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);
    }
}
 
開發者ID:fcrepo4,項目名稱:fcrepo4,代碼行數:27,代碼來源:LdpTestSuiteIT.java

示例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);
    }
}
 
開發者ID:fcrepo4,項目名稱:fcrepo4,代碼行數:25,代碼來源:LdpTestSuiteIT.java

示例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;
}
 
開發者ID:philipwhiuk,項目名稱:q-mail,代碼行數:9,代碼來源:WebDavStoreTest.java

示例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;
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:15,代碼來源:MyHttpStack.java

示例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;
}
 
開發者ID:wangzhaosheng,項目名稱:publicProject,代碼行數:14,代碼來源:OkHttpStack.java

示例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;
}
 
開發者ID:geminiKim,項目名稱:logregator,代碼行數:8,代碼來源:HttpCollectorHandlerTest.java


注:本文中的org.apache.http.entity.BasicHttpEntity.setContent方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。