Asynchronous HTTP Client for Java

后端 未结 13 1628
梦谈多话
梦谈多话 2020-12-01 05:01

As a relative newbie in the Java world, I am finding many things frustratingly obtuse to accomplish that are relatively trivial in many other frameworks. A primary example i

13条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-01 05:26

    there are libraries and frameworks written on top of NIO/Netty - RxNetty and Vertx which can be helpful to write asynchronous HTTP client

    Below is an example code using vertx

    public class Client extends AbstractVerticle {
    
      @Override
      public void start() throws Exception {
        //lambda callback would be called when the response comes back
        vertx.createHttpClient().getNow(8080, "localhost", "/", resp -> {
          System.out.println("Got response " + resp.statusCode());
          resp.bodyHandler(body -> {
            System.out.println("Got data " + body.toString("ISO-8859-1"));
          });
        });
       //this code statement will execute before response comes back
       System.out.println("I am not blocked");
      }
    }
    

    you can find the full blown example code from here

提交回复
热议问题