zoukankan      html  css  js  c++  java
  • vertx简单客户端创建

    import java.util.HashMap;
    import java.util.Map;
    
    import com.yunva.vertx.test.vertproject.util.JsonUtil;
    
    import io.vertx.core.http.HttpClientOptions;
    import io.vertx.rxjava.core.Vertx;
    import io.vertx.rxjava.core.http.HttpClient;
    import io.vertx.rxjava.core.http.HttpClientRequest;
    
    public class TestClient {
    
    private Vertx vertx = Vertx.vertx();
    
    public HttpClient getClient (){
    HttpClientOptions options = new HttpClientOptions();
    options.setDefaultPort(8080).setDefaultHost("127.0.0.1").setKeepAlive(false);
    HttpClient client = vertx.createHttpClient(options);
    return client;
    }
    
    public static void main(String[] args){
    TestClient tct = new TestClient();
    HttpClient client = tct.getClient();
    Map<String, Object> map = new HashMap<>();
    map.put("name", "shad");
    map.put("age", 55);
    map.put("school", "xinhuazhongxue");
    String body = JsonUtil.objectToJson(map);
    client.post("some-uri", response -> {//
    //do something
    System.out.println("Received response with status code " + response.statusCode());
    response.handler(buffer -> {//读取返回内容
    System.out.println(buffer.toString());
    });
    }).
    putHeader("content-length", "1000").//请求头
    putHeader("content-type", "text/plain").
    setTimeout(2000).//请求超时
    write(body).//请求内容
    end();
    
    /*HttpClientRequest request = client.post("/some-url");
    request.handler(response -> {//请求处理
    
    });
    request.exceptionHandler(err -> {//请求异常处理
    System.out.println("Received response with status code " + err.getMessage());
    err.printStackTrace();
    });*/
    }
    
    }
  • 相关阅读:
    DHTML 动态效果100例
    Apache 发布网站
    Django笔记 3
    JDBC
    Android国行手机使用Google Play Store
    Cloud9 使用 GitHub
    maven
    linux 下查看系统信息的一些命令
    C++成员变量指针和成员函数指针
    SIP 协议
  • 原文地址:https://www.cnblogs.com/canmeng-cn/p/5939009.html
Copyright © 2011-2022 走看看