コード例 #1
0
 @Test(groups = "standalone")
 public void testQueryParameters() throws Exception {
   try (AsyncHttpClient client = asyncHttpClient()) {
     Future<Response> f =
         client
             .prepareGet("http://127.0.0.1:" + port1 + "/foo")
             .addHeader("Accepts", "*/*")
             .execute();
     Response resp = f.get(3, TimeUnit.SECONDS);
     assertNotNull(resp);
     assertEquals(resp.getStatusCode(), 400);
     assertEquals(resp.getResponseBody(), BAD_REQUEST_STR);
   }
 }
コード例 #2
0
  @Test(groups = "online", enabled = false)
  public void testAHC62Com() throws Exception {
    try (AsyncHttpClient c = asyncHttpClient(config().setFollowRedirect(true))) {
      Response response =
          c.prepareGet("http://api.crunchbase.com/v/1/financial-organization/kinsey-hills-group.js")
              .execute(
                  new AsyncHandler<Response>() {

                    private Response.ResponseBuilder builder = new Response.ResponseBuilder();

                    public void onThrowable(Throwable t) {
                      t.printStackTrace();
                    }

                    public State onBodyPartReceived(HttpResponseBodyPart bodyPart)
                        throws Exception {
                      System.out.println(bodyPart.getBodyPartBytes().length);
                      builder.accumulate(bodyPart);

                      return State.CONTINUE;
                    }

                    public State onStatusReceived(HttpResponseStatus responseStatus)
                        throws Exception {
                      builder.accumulate(responseStatus);
                      return State.CONTINUE;
                    }

                    public State onHeadersReceived(HttpResponseHeaders headers) throws Exception {
                      builder.accumulate(headers);
                      return State.CONTINUE;
                    }

                    public Response onCompleted() throws Exception {
                      return builder.build();
                    }
                  })
              .get(10, TimeUnit.SECONDS);
      assertNotNull(response);
      assertTrue(response.getResponseBody().length() >= 3870);
    }
  }