每次都需要 HttpPost 中的授权吗?

Does Authorization in HttpPost is required everytime?

我有一个简单的 http post 代码。我有一些基本问题。

CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Authorization", "Basic username:password");
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
httpPost.setEntity(entity);
CloseableHttpResponse response = httpClient.execute(httpPost); 
httpClient.close();

是否需要为每个 post 请求指定授权?一旦为第一个请求建立连接,是否有办法为后续 post 请求重用授权凭据?服务器是否在一段时间内保持特定客户端的授权有效?

取决于您的 http 服务器代码及其处理每个请求的方式;最简单的方法可能是在服务器的响应中设置一个 cookie(或其他标识符值),并在第一次成功授权后在连续请求中发送该值;