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

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

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

Response.getResponseBody介绍

[英]Return the entire response body as a String.
[中]将整个响应主体作为字符串返回。

代码示例

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

public String getResponseBody() {
 return response.getResponseBody();
}

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

@Test
public void testPutEmptyBody() throws Exception {
 try (AsyncHttpClient ahc = asyncHttpClient()) {
  Response response = ahc.preparePut(getTargetUrl()).setBody("String").execute().get();
  assertNotNull(response);
  assertEquals(response.getStatusCode(), 204);
  assertEquals(response.getResponseBody(), "");
  assertNotNull(response.getResponseBodyAsStream());
  assertEquals(response.getResponseBodyAsStream().read(), -1);
 }
}

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

public String getResponseBody(Charset charset) {
 return response.getResponseBody(charset);
}

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

@Test
public void zeroCopyPutTest() throws IOException, ExecutionException, InterruptedException {
 try (AsyncHttpClient client = asyncHttpClient()) {
  Future<Response> f = client.preparePut("http://localhost:" + port1 + "/").setBody(SIMPLE_TEXT_FILE).execute();
  Response resp = f.get();
  assertNotNull(resp);
  assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
  assertEquals(resp.getResponseBody(), SIMPLE_TEXT_FILE_STRING);
 }
}

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

@Test(groups = "standalone")
public void testQueryParameters() throws Exception {
 try (AsyncHttpClient client = asyncHttpClient()) {
  Future<Response> f = client.prepareGet("http://localhost:" + port1 + "/foo").addHeader("Accepts", "*/*").execute();
  Response resp = f.get(3, TimeUnit.SECONDS);
  assertNotNull(resp);
  assertEquals(resp.getStatusCode(), 400);
  assertEquals(resp.getResponseBody(), BAD_REQUEST_STR);
 }
}

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

@Test
public void Expect100Continue() throws Exception {
 try (AsyncHttpClient client = asyncHttpClient()) {
  Future<Response> f = client.preparePut("http://localhost:" + port1 + "/")
      .setHeader(EXPECT, HttpHeaderValues.CONTINUE)
      .setBody(SIMPLE_TEXT_FILE)
      .execute();
  Response resp = f.get();
  assertNotNull(resp);
  assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
  assertEquals(resp.getResponseBody(), SIMPLE_TEXT_FILE_STRING);
 }
}

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

@Test
 public void urlWithoutSlashTest() throws Exception {
  try (AsyncHttpClient c = asyncHttpClient()) {
   String body = "hello there";
   Response response = c.preparePost(String.format("http://localhost:%d/foo/test", port1)).setBody(body).setHeader("Content-Type", "text/html").execute().get(TIMEOUT, TimeUnit.SECONDS);
   assertEquals(response.getResponseBody(), body);
  }
 }
}

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

@Test
public void basicAuthInputStreamTest() throws IOException, ExecutionException, TimeoutException, InterruptedException {
 try (AsyncHttpClient client = asyncHttpClient()) {
  Future<Response> f = client.preparePost(getTargetUrl())
      .setBody(new ByteArrayInputStream("test".getBytes()))
      .setRealm(basicAuthRealm(USER, ADMIN).build())
      .execute();
  Response resp = f.get(30, TimeUnit.SECONDS);
  assertNotNull(resp);
  assertNotNull(resp.getHeader("X-Auth"));
  assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
  assertEquals(resp.getResponseBody(), "test");
 }
}

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

protected void execute(String body) throws IOException, InterruptedException, ExecutionException {
  try (AsyncHttpClient client = asyncHttpClient()) {
   BoundRequestBuilder r = client.preparePost(getTargetUrl()).setBody(body).setCharset(UTF_8);
   Future<Response> f = r.execute();
   Response resp = f.get();
   assertEquals(resp.getStatusCode(), 200);
   assertEquals(body, resp.getResponseBody(UTF_8));
  }
 }
}

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

@Test
public void basicAuthAsyncConfigTest() throws Exception {
 try (AsyncHttpClient client = asyncHttpClient(config().setRealm(basicAuthRealm(USER, ADMIN)))) {
  Future<Response> f = client.preparePost(getTargetUrl())
      .setBody(SIMPLE_TEXT_FILE_STRING)
      .execute();
  Response resp = f.get(3, TimeUnit.SECONDS);
  assertNotNull(resp);
  assertNotNull(resp.getHeader("X-Auth"));
  assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
  assertEquals(resp.getResponseBody(), SIMPLE_TEXT_FILE_STRING);
 }
}

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

@Test
public void basicAuthFileTest() throws Exception {
 try (AsyncHttpClient client = asyncHttpClient()) {
  Future<Response> f = client.preparePost(getTargetUrl())
      .setBody(SIMPLE_TEXT_FILE)
      .setRealm(basicAuthRealm(USER, ADMIN).build())
      .execute();
  Response resp = f.get(3, TimeUnit.SECONDS);
  assertNotNull(resp);
  assertNotNull(resp.getHeader("X-Auth"));
  assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
  assertEquals(resp.getResponseBody(), SIMPLE_TEXT_FILE_STRING);
 }
}

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

@Test
public void basicAuthFileNoKeepAliveTest() throws Exception {
 try (AsyncHttpClient client = asyncHttpClient(config().setKeepAlive(false))) {
  Future<Response> f = client.preparePost(getTargetUrl())
      .setBody(SIMPLE_TEXT_FILE)
      .setRealm(basicAuthRealm(USER, ADMIN).build())
      .execute();
  Response resp = f.get(3, TimeUnit.SECONDS);
  assertNotNull(resp);
  assertNotNull(resp.getHeader("X-Auth"));
  assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
  assertEquals(resp.getResponseBody(), SIMPLE_TEXT_FILE_STRING);
 }
}

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

@Test
public void multipleRequestsTest() throws Exception {
 try (AsyncHttpClient c = asyncHttpClient()) {
  String body = "hello there";
  // once
  Response response = c.preparePost(getTargetUrl()).setBody(body).setHeader("Content-Type", "text/html").execute().get(TIMEOUT, TimeUnit.SECONDS);
  assertEquals(response.getResponseBody(), body);
  // twice
  response = c.preparePost(getTargetUrl()).setBody(body).setHeader("Content-Type", "text/html").execute().get(TIMEOUT, TimeUnit.SECONDS);
  assertEquals(response.getResponseBody(), body);
 }
}

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

@Test
public void getEmptyBody() throws Throwable {
 withClient().run(client ->
  withServer(server).run(server -> {
   server.enqueueOk();
   Response response = client.prepareGet(getTargetUrl()).execute(new AsyncCompletionHandlerAdapter())
       .get(TIMEOUT, SECONDS);
   assertTrue(response.getResponseBody().isEmpty());
  }));
}

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

@Test
public void regular302LosesBody() throws Exception {
 try (AsyncHttpClient c = asyncHttpClient(config().setFollowRedirect(true))) {
  String body = "hello there";
  String contentType = "text/plain; charset=UTF-8";
  Response response = c.preparePost(getTargetUrl()).setHeader(CONTENT_TYPE, contentType).setBody(body).setHeader("X-REDIRECT", "302").execute().get(TIMEOUT, TimeUnit.SECONDS);
  assertEquals(response.getResponseBody(), "");
  assertNull(receivedContentType);
 }
}

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

@Test
public void regular301LosesBody() throws Exception {
 try (AsyncHttpClient c = asyncHttpClient(config().setFollowRedirect(true))) {
  String body = "hello there";
  String contentType = "text/plain; charset=UTF-8";
  Response response = c.preparePost(getTargetUrl()).setHeader(CONTENT_TYPE, contentType).setBody(body).setHeader("X-REDIRECT", "301").execute().get(TIMEOUT, TimeUnit.SECONDS);
  assertEquals(response.getResponseBody(), "");
  assertNull(receivedContentType);
 }
}

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

@Test
 public void regular307KeepsBody() throws Exception {
  try (AsyncHttpClient c = asyncHttpClient(config().setFollowRedirect(true))) {
   String body = "hello there";
   String contentType = "text/plain; charset=UTF-8";

   Response response = c.preparePost(getTargetUrl()).setHeader(CONTENT_TYPE, contentType).setBody(body).setHeader("X-REDIRECT", "307").execute().get(TIMEOUT, TimeUnit.SECONDS);
   assertEquals(response.getResponseBody(), body);
   assertEquals(receivedContentType, contentType);
  }
 }
}

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

@Test
public void postFileOverHttps() throws Throwable {
 logger.debug(">>> postBodyOverHttps");
 withClient(config().setSslEngineFactory(createSslEngineFactory())).run(client ->
  withServer(server).run(server -> {
   server.enqueueEcho();
   Response resp = client.preparePost(getTargetUrl()).setBody(SIMPLE_TEXT_FILE).setHeader(CONTENT_TYPE, "text/html").execute().get();
   assertNotNull(resp);
   assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
   assertEquals(resp.getResponseBody(), SIMPLE_TEXT_FILE_STRING);
  }));
 logger.debug("<<< postBodyOverHttps");
}

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

@Test
public void regular302StrictKeepsBody() throws Exception {
 try (AsyncHttpClient c = asyncHttpClient(config().setFollowRedirect(true).setStrict302Handling(true))) {
  String body = "hello there";
  String contentType = "text/plain; charset=UTF-8";
  Response response = c.preparePost(getTargetUrl()).setHeader(CONTENT_TYPE, contentType).setBody(body).setHeader("X-REDIRECT", "302").execute().get(TIMEOUT, TimeUnit.SECONDS);
  assertEquals(response.getResponseBody(), body);
  assertEquals(receivedContentType, contentType);
 }
}

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

@Test
public void propFindWebDavTest() throws InterruptedException, IOException, ExecutionException {
 try (AsyncHttpClient c = asyncHttpClient()) {
  Request mkcolRequest = new RequestBuilder("MKCOL").setUrl(getTargetUrl()).build();
  Response response = c.executeRequest(mkcolRequest).get();
  assertEquals(response.getStatusCode(), 201);
  Request putRequest = put(getTargetUrl() + "/Test.txt").setBody("this is a test").build();
  response = c.executeRequest(putRequest).get();
  assertEquals(response.getStatusCode(), 201);
  Request propFindRequest = new RequestBuilder("PROPFIND").setUrl(getTargetUrl() + "/Test.txt").build();
  response = c.executeRequest(propFindRequest).get();
  assertEquals(response.getStatusCode(), 207);
  assertTrue(response.getResponseBody().contains("HTTP/1.1 200 OK"), "Got " + response.getResponseBody());
 }
}

相关文章