首页 > 解决方案 > 无法为我的发帖请求提供用户名密码

问题描述

我是 Java 和 Vert.x 的新手

我正在尝试实现基本身份验证,以便我可以在发送发布请求之前登录远程服务器

我收到编译器错误。HttpRequest 类型的方法 basicAuthentication(String, String) 未定义我在做什么错。

package com.aexp.csrt.qs.cb.resources;
import com.aexp.csrt.qs.models.cb.passTrou.SourceQueryModel;
import io.vertx.reactivex.ext.web.RoutingContext;
import io.vertx.reactivex.core.Vertx;
import io.vertx.ext.web.client.WebClient;
import io.vertx.ext.web.client.WebClientOptions;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.client.HttpResponse;
import io.vertx.core.buffer.Buffer;

public class QueryExecutorFTS implements QueryExecutor {
    private WebClient webClient;

    @Override
    public void executeQuery(SourceQueryModel Qm, RoutingContext rc) {

        WebClientOptions options = new WebClientOptions().setMaxPoolSize(10).setConnectTimeout(5000)
                .setVerifyHost(false);

        JsonObject jreq = new JsonObject(Qm.getSourceQuery().getSourceDsl().getQuery());

        Vertx vertx = rc.vertx();
        webClient = WebClient.create(vertx.getDelegate(), options);

         webClient
         .basicAuthentication("myid", "mypassword")
        .post(8094, "lpdospdb51079.phx.aexp.com", "/api/index/Text_search_name_idx/query")
        .sendJsonObject(jreq,
                ar -> {
                    if (ar.succeeded()) {
                        HttpResponse<Buffer> response = ar.result();
                        rc.response().setStatusCode(200).end(response.bodyAsString());
                    } else {
                        ar.cause().printStackTrace();
                        rc
                              .response()
                              .setStatusCode(500)
                              .setStatusMessage(ar.cause().getMessage())
                              .end();
                    }
                })
        ;


    }

}

标签: javavert.x

解决方案


您的方法调用顺序不正确。
应该:

webClient         
    .post(8094, "lpdospdb51079.phx.aexp.com", "/api/index/Text_search_name_idx/query")
    .basicAuthentication("myid", "mypassword")...

请注意,此 API 在3.5.3. 您需要3.6.3使用它: https ://github.com/vert-x3/vertx-web/blame/5e54ad321b76eaeb4c19ca1706f2f90376a0534f/vertx-web-client/src/main/java/io/vertx/ext/web/客户端/HttpRequest.java#L159


推荐阅读