HttpResponse 中的错误状态代码

Wrong status code in HttpResponse

起初 - 我是 Java 的新手。我正在开发一个使用 Shiro 的应用程序,并且我已经为注销定义了一个 REST。它内部有一个重定向,在 Chrome:

中使用开发工具时给我 302 Found Status Code
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 中,我使用的是 Basic Auth,但在 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);