1

3.3 我尝试以异步方式执行 rest api 调用。为此,我尝试使用 httpClient,`final HttpClient httpClient = vertx.createHttpClient();

        new HttpGet(uri);

        httpClient.requestAbs(apiMethod.getHttpMethod(), uri, resultHandler -> {
            resultHandler.statusCode();
            if (resultHandler.statusCode() == 200) {
                environment.setStatus(ApiStatus.OK);
                apiMethod.setStatus(ApiStatus.OK);
            } else {
                environment.setStatus(ApiStatus.ERROR);
                apiMethod.setStatus(ApiStatus.ERROR);
            }
            resultHandler.bodyHandler(buffer -> {
                logger.debug("Output from Server .... \n");
                buffer.toString();
            });
        }).exceptionHandler(handlerExc -> {
            logger.error(" handle exception:" + handlerExc.getMessage(), handlerExc.getCause());
            apiMethod.setStatus(ApiStatus.ERROR);
            environment.setStatus(ApiStatus.ERROR);
            apiMethod.setCallingResult(handlerExc.getMessage());

        }).putHeader("content-type", "application/json").end();`

我仍然收到错误消息:连接已关闭。当我卷曲请求或使用我的浏览器/邮递员时,它工作正常。谁能知道我错在哪里?是网络问题吗?最好的祝福,

4

1 回答 1

1

这是完整的代码

    final HttpClientOptions options = new HttpClientOptions();
    options.setTrustAll(true);
    final String proxyUrl = "yourProxyUrl.net";
    options.setSsl(true);
    int port = 443;
    final ProxyOptions proxyOptions = new ProxyOptions().setHost(proxyUrl).setPort(port);
    options.setProxyOptions(proxyOptions);
    options.setDefaultPort(port);
    HttpClient httpClient= vertx.createHttpClient(options);

    httpClient.requestAbs(apiMethod.getHttpMethod(), uri, resultHandler -> {
        resultHandler.statusCode();
        if (resultHandler.statusCode() == 200) {
            environment.setStatus(ApiStatus.OK);
            apiMethod.setStatus(ApiStatus.OK);
        } else {
            environment.setStatus(ApiStatus.ERROR);
            apiMethod.setStatus(ApiStatus.ERROR);
        }
        resultHandler.bodyHandler(buffer -> {
            logger.debug("Output from Server .... \n");
            buffer.toString();
        });
    }).exceptionHandler(handlerExc -> {
        logger.error(" handle exception:" + handlerExc.getMessage(), handlerExc.getCause());
        apiMethod.setStatus(ApiStatus.ERROR);
        environment.setStatus(ApiStatus.ERROR);
        apiMethod.setCallingResult(handlerExc.getMessage());

    }).putHeader("content-type", "application/json").end();
于 2016-11-02T18:01:32.640 回答