org.asynchttpclient.Response.getResponseBodyAsBytes()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(9.5k)|赞(0)|评价(0)|浏览(111)

本文整理了Java中org.asynchttpclient.Response.getResponseBodyAsBytes方法的一些代码示例,展示了Response.getResponseBodyAsBytes的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Response.getResponseBodyAsBytes方法的具体详情如下:
包路径:org.asynchttpclient.Response
类名称:Response
方法名:getResponseBodyAsBytes

Response.getResponseBodyAsBytes介绍

[英]Return the entire response body as a byte[].
[中]以字节[]的形式返回整个响应体。

代码示例

代码示例来源:origin: AsyncHttpClient/async-http-client

@Override
public byte[] getResponseBodyAsBytes() {
 return response.getResponseBodyAsBytes();
}

代码示例来源:origin: apache/pulsar

T data = ObjectMapperFactory.getThreadLocal().readValue(response.getResponseBodyAsBytes(), clazz);
  future.complete(data);
} catch (Exception e) {

代码示例来源:origin: AsyncHttpClient/async-http-client

private void waitForAndAssertResponse(ListenableFuture<Response> responseFuture) throws InterruptedException, java.util.concurrent.ExecutionException {
  Response response = responseFuture.get();
  if (500 == response.getStatusCode()) {
   logger.debug("==============\n" +
       "500 response from call\n" +
       "Headers:" + response.getHeaders() + "\n" +
       "==============\n");
   assertEquals(response.getStatusCode(), 500, "Should have 500 status code");
   assertTrue(response.getHeader("X-Exception").contains("invalid.chunk.length"), "Should have failed due to chunking");
   fail("HARD Failing the test due to provided InputStreamBodyGenerator, chunking incorrectly:" + response.getHeader("X-Exception"));
  } else {
   assertEquals(response.getResponseBodyAsBytes(), LARGE_IMAGE_BYTES);
  }
 }
}

代码示例来源:origin: AsyncHttpClient/async-http-client

private void expectSuccess(Function<BoundRequestBuilder, BoundRequestBuilder> f) throws Exception {
 File file = createTempFile(1024 * 1024);
 try (AsyncHttpClient client = asyncHttpClient()) {
  for (int i = 0; i < 20; i++) {
   Response response = f.apply(client.preparePut(getTargetUrl())
       .addBodyPart(new FilePart("test", file, APPLICATION_OCTET_STREAM.toString(), UTF_8)))
       .execute().get();
   assertEquals(response.getStatusCode(), 200);
   assertEquals(response.getResponseBodyAsBytes().length, Integer.valueOf(response.getHeader("X-" + CONTENT_LENGTH)).intValue());
  }
 }
}

代码示例来源:origin: AsyncHttpClient/async-http-client

@Test
public void postLargeFileOverHttps() throws Throwable {
 logger.debug(">>> postLargeFileOverHttps");
 withClient(config().setSslEngineFactory(createSslEngineFactory())).run(client ->
  withServer(server).run(server -> {
   server.enqueueEcho();
   Response resp = client.preparePost(getTargetUrl()).setBody(LARGE_IMAGE_FILE).setHeader(CONTENT_TYPE, "image/png").execute().get();
   assertNotNull(resp);
   assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
   assertEquals(resp.getResponseBodyAsBytes().length, LARGE_IMAGE_FILE.length());
  }));
 logger.debug("<<< postLargeFileOverHttps");
}

代码示例来源:origin: AsyncHttpClient/async-http-client

/**
 * Converts async-http-client response to okhttp response.
 *
 * @param asyncHttpClientResponse async-http-client response
 * @return okhttp response.
 * @throws NullPointerException in case of null arguments
 */
private Response toOkhttpResponse(org.asynchttpclient.Response asyncHttpClientResponse) {
 // status code
 val rspBuilder = new Response.Builder()
     .request(request())
     .protocol(Protocol.HTTP_1_1)
     .code(asyncHttpClientResponse.getStatusCode())
     .message(asyncHttpClientResponse.getStatusText());
 // headers
 if (asyncHttpClientResponse.hasResponseHeaders()) {
  asyncHttpClientResponse.getHeaders().forEach(e -> rspBuilder.header(e.getKey(), e.getValue()));
 }
 // body
 if (asyncHttpClientResponse.hasResponseBody()) {
  val contentType = asyncHttpClientResponse.getContentType() == null
      ? null : MediaType.parse(asyncHttpClientResponse.getContentType());
  val okHttpBody = ResponseBody.create(contentType, asyncHttpClientResponse.getResponseBodyAsBytes());
  rspBuilder.body(okHttpBody);
 } else {
  rspBuilder.body(EMPTY_BODY);
 }
 return rspBuilder.build();
}

代码示例来源:origin: AsyncHttpClient/async-http-client

@Test
public void testStreamingPutImage() throws Exception {
 try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) {
  Response response = client.preparePut(getTargetUrl()).setBody(createAsyncPublisher(LARGE_IMAGE_BYTES, 2342))
      .execute().get();
  assertEquals(response.getStatusCode(), 200);
  assertEquals(response.getResponseBodyAsBytes(), LARGE_IMAGE_BYTES);
 }
}

代码示例来源:origin: AsyncHttpClient/async-http-client

@Test
public void testAsyncStreamingPutImage() throws Exception {
 // test that streaming works with a publisher that does not invoke onSubscription synchronously from subscribe
 try (AsyncHttpClient client = asyncHttpClient(config().setRequestTimeout(100 * 6000))) {
  Response response = client.preparePut(getTargetUrl()).setBody(createPublisher(LARGE_IMAGE_BYTES, 2342))
      .execute().get();
  assertEquals(response.getStatusCode(), 200);
  assertEquals(response.getResponseBodyAsBytes(), LARGE_IMAGE_BYTES);
 }
}

代码示例来源:origin: AsyncHttpClient/async-http-client

@Test
public void defaultRequestBodyEncodingIsUtf8() throws Throwable {
 withClient().run(client ->
  withServer(server).run(server -> {
   server.enqueueEcho();
   Response response = client.preparePost(getTargetUrl())
       .setBody("\u017D\u017D\u017D\u017D\u017D\u017D")
       .execute().get();
   assertEquals(response.getResponseBodyAsBytes(), "\u017D\u017D\u017D\u017D\u017D\u017D".getBytes(UTF_8));
  }));
}

代码示例来源:origin: AsyncHttpClient/async-http-client

@Test
public void emptyResponseBodyBytesAreEmpty() throws Throwable {
 withClient().run(client ->
  withServer(server).run(server -> {
   server.enqueueEcho();
   Response response = client.prepareGet(getTargetUrl()).execute().get();
   assertEquals(response.getStatusCode(), 200);
   assertEquals(response.getResponseBodyAsBytes(), new byte[]{});
  }));
}

代码示例来源:origin: AsyncHttpClient/async-http-client

assertEquals(response.getStatusCode(), 200, "HTTP response was invalid on first request.");
byte[] responseBody = response.getResponseBodyAsBytes();
assertEquals(Integer.valueOf(response.getHeader("X-" + CONTENT_LENGTH)).intValue(),
    LARGE_IMAGE_BYTES.length, "Server side payload length invalid");
responseBody = response.getResponseBodyAsBytes();
assertEquals(Integer.valueOf(response.getHeader("X-" + CONTENT_LENGTH)).intValue(),
    LARGE_IMAGE_BYTES.length, "Server side payload length invalid");

代码示例来源:origin: com.typesafe.play/play-java-ws

/**
 * Get the response body as a byte array
 *
 * @return The byte array
 */
@Override
public byte[] asByteArray() {
  return ahcResponse.getResponseBodyAsBytes();
}

代码示例来源:origin: org.asynchttpclient/async-http-client-api

@Override
public byte[] getResponseBodyAsBytes() throws IOException {
  return response.getResponseBodyAsBytes();
}

代码示例来源:origin: com.commercetools.sdk.jvm.core/sdk-http-ahc-2_0

private byte[] getResponseBodyAsBytes(final Response response) {
  return response.getResponseBodyAsBytes();
}

代码示例来源:origin: commercetools/commercetools-jvm-sdk

private byte[] getResponseBodyAsBytes(final Response response) {
  return response.getResponseBodyAsBytes();
}

代码示例来源:origin: commercetools/commercetools-jvm-sdk

private byte[] getResponseBodyAsBytes(final Response response) {
  return response.getResponseBodyAsBytes();
}

代码示例来源:origin: cloudbees/zendesk-java-client

@Override
  public List<ArticleAttachments> onCompleted(Response response) throws Exception {
    logResponse(response);
    if (isStatus2xx(response)) {
      JsonNode responseNode = mapper.readTree(response.getResponseBodyAsBytes());
      List<ArticleAttachments> values = new ArrayList<>();
      for (JsonNode node : responseNode.get(name)) {
        values.add(mapper.convertValue(node, ArticleAttachments.class));
      }
      return values;
    } else if (isRateLimitResponse(response)) {
      throw new ZendeskResponseRateLimitException(response);
    }
    throw new ZendeskResponseException(response);
  }
};

代码示例来源:origin: cloudbees/zendesk-java-client

@Override
  public List<Target> onCompleted(Response response) throws Exception {
    logResponse(response);
    if (isStatus2xx(response)) {
      JsonNode responseNode = mapper.readTree(response.getResponseBodyAsBytes());
      setPagedProperties(responseNode, null);
      List<Target> values = new ArrayList<>();
      for (JsonNode node : responseNode.get(name)) {
        Class<? extends Target> clazz = targetTypes.get(node.get("type").asText());
        if (clazz != null) {
          values.add(mapper.convertValue(node, clazz));
        }
      }
      return values;
    } else if (isRateLimitResponse(response)) {
      throw new ZendeskResponseRateLimitException(response);
    }
    throw new ZendeskResponseException(response);
  }
};

代码示例来源:origin: cloudbees/zendesk-java-client

@Override
  public List<T> onCompleted(Response response) throws Exception {
    logResponse(response);
    if (isStatus2xx(response)) {
      JsonNode responseNode = mapper.readTree(response.getResponseBodyAsBytes());
      setPagedProperties(responseNode, clazz);
      List<T> values = new ArrayList<>();
      for (JsonNode node : responseNode.get(name)) {
        values.add(mapper.convertValue(node, clazz));
      }
      return values;
    } else if (isRateLimitResponse(response)) {
      throw new ZendeskResponseRateLimitException(response);
    }
    throw new ZendeskResponseException(response);
  }
}

代码示例来源:origin: com.tomitribe.tribestream/tribestream-container

private boolean handleResponse(final Response proxyResponse) throws IOException {
  final Integer statusCode = (proxyResponse != null) ? proxyResponse.getStatusCode() : null;
  if (responseHandled(statusCode)) return true;
  // Send the content to the client
  final byte[] entity = proxyResponse.getResponseBodyAsBytes();
  setXProxyElapsed();
  context.getEventHandler().info("proxy", "Copying proxy response finished");
  final BufferedInputStream bufferedInputStream = new BufferedInputStream(new ByteArrayInputStream(entity));
  final OutputStream outputStreamClientResponse = context.getResponse().getOutputStream();
  long size = 0;
  int intNextByte;
  while ((intNextByte = bufferedInputStream.read()) != -1) {
    outputStreamClientResponse.write(intNextByte);
    size++;
  }
  flushResponseBytes(size);
  return true;
}

相关文章