gpt4 book ai didi

java - Vertx客户端Http : Connection closed when perform rest call

转载 作者:行者123 更新时间:2023-11-30 02:45:14 24 4
gpt4 key购买 nike

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();`

我仍然收到错误:连接已关闭。当我 curl 请求或使用我的浏览器/ postman 时,它工作正常。谁能知道我错在哪里吗?是网络问题吗?谨致问候,

最佳答案

这是完整的代码

    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();

关于java - Vertx客户端Http : Connection closed when perform rest call,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40373766/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com