gpt4 book ai didi

java - HttpResponse 中的状态代码错误

转载 作者:行者123 更新时间:2023-12-01 09:11:54 29 4
gpt4 key购买 nike

首先 - 我对 Java 还很陌生。我正在开发一个使用 Shiro 的应用程序,并且我已经定义了用于注销的 REST。它内部有一个重定向,在 Chrome 中使用开发工具时会显示 302 Found 状态代码:

return Response.temporaryRedirect(redirectionAddress).build();

问题是,我无法使用 Postman 或仅通过在测试中调用它来获取此状态:

HttpUriRequest request = new HttpGet("http://localhost:8080/shop/logout/);
HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);

调用其余部分后,我只得到 200 OK 代码,没有 302 重定向。大概是服务器端没有 session 时的响应。在 Postman 中,我使用基本身份验证,但在 HttpGet 中没有授权,因为我不确定如何执行此操作。我尝试添加 header ,但它也不起作用:

httpRequestBase.addHeader("Authorization", "Basic " + auth);

我应该首先调用登录休息然后注销吗?是否有任何选项可以在不登录的情况下测试注销?

最佳答案

Apache HttpClient 进行自动重定向处理,请参阅 HttpClient Tutorial :

1.7. Redirect handling

HttpClient handles all types of redirects automatically, except those explicitly prohibited by the HTTP specification as requiring user intervention. See Other (status code 303) redirects on POST and PUT requests are converted to GET requests as required by the HTTP specification.

但您可以禁用它,请参阅 HttpClientBuilder#disableRedirectHandling :

Disables automatic redirect handling.

您修改后的代码:

HttpUriRequest request = new HttpGet("http://localhost:8080/shop/logout/");
HttpResponse httpResponse = HttpClientBuilder.create().disableRedirectHandling().build().execute(request);

关于java - HttpResponse 中的状态代码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40866956/

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