簡體   English   中英

Jersey HTTP響應200可以,但是返回內容錯誤(檢查請求)

[英]Jersey HTTP response 200 OK but returning content wrong (checking request)

這是我昨天提出的問題的后續問題。

輸入正確的用戶名和密碼后,登錄頁面應該重定向到主頁,服務器端返回一個空字符串( "" )。 如果任何一個都不正確,則服務器端代碼將返回"Username or Password are incorrect"

頁面功能運行良好,但是當我使用正確的用戶名和密碼對使用客戶端代碼進行測試時,它返回"Username or Password are incorrect" ,響應返回200OK。

下面是我的客戶端代碼:

public static void main(String[] args){
    ClientConfig config = new ClientConfig();
    Client client = ClientBuilder.newClient(config);
    WebTarget target = client.target("http://localhost:8080
                               /qa-automation-console").path("authenticate");
    Form form = new Form();
    form.param("username", "username");
    form.param("password", "password");
    Response response = target.request().post(Entity.form(form));
     //The response was 200OK.
    System.out.println(response.readEntity(String.class));
}

現在,我懷疑我編寫的客戶端代碼不正確,而不是其他問題,包括HTML和Web.xml依賴關系,並且在發送請求時,它不包含正確的用戶名和密碼。 服務器端代碼如下:

@POST
@Produces("text/plain")
@Path("authenticate")
public String authenticate(@Context HttpServletRequest req, @QueryParam("username") 
                      String username, @QueryParam("password") String password) 
                                                          throws Exception {
    Environments environments = new DefaultConfigurationBuilder().build();
    final ALMProfile profile = new ALMProfile();
    profile.setUrl(environments.getAutomation().getAlmProfile().getUrl());
    profile.setUsername(username);
    if ( !Strings.isNullOrEmpty(password) ) {
        String encryptedPassword = EncryptionUtils.encrypt(password);
        profile.setPassword(encryptedPassword);
    }
    try (ALMConnection connection = new ALMConnection(profile);) {
        if (connection.getOtaConnector().connected()) {
            req.getSession(true).setAttribute("username", username);
            req.getSession(true).setAttribute("password", profile.getPassword());
            return "";
        }
    } catch (Exception e) {
        e.printStackTrace();
        return "Username or Password are incorrect";
    }
    return "Username or Password are incorrect";
}

有人可以指出客戶代碼是否提交了正確的請求?

首先,您可能需要檢查堆棧跟蹤中的內容。 以及usernamepassword的值。 我懷疑它們為空。

其次,我認為問題來自@QueryParam批注。

您必須改為使用@FormParam

當您的網址包含以下參數時,可以使用QueryParam:

www.test.com/test?username=test

在表單中發送數據時,必須使用@FormParam批注

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM